Published on :
Data Management CREATION_INTERNE

Picture Formats and Date Directives

This code is also available in: Deutsch Español Français
Awaiting validation
This script explores the advanced features of the PROC FORMAT procedure with the PICTURE statement. It demonstrates how to use strftime-like directives (e.g., %Y, %m) to format datetime values. It includes examples for standard formatting, handling rounding, using 'datetime_util' to represent midnight as 24:00:00, and formatting durations (intervals).
Data Analysis

Type : CREATION_INTERNE


All data is dynamically generated in DATA _NULL_ steps using datetime literals.

1 Code Block
PROC FORMAT
Explanation :
Creation of a 'dbdate' picture format to display a datetime value in standard YYYY-MM-DD:HH:MM:SS format.
Copied!
1title1 '12.2.1 Picture Formats';
2title2 'Using Date Directives';
3PROC FORMAT;
4 picture dbdate
5 other = '%Y-%0m-%0d:%0H:%0M:%0S' (datatype=datetime);
6 RUN;
2 Code Block
DATA STEP Data
Explanation :
Application of the 'dbdate' format in a Data _NULL_ step and via a macro variable with %sysfunc.
Copied!
1DATA _null_;
2 now = '11sep2010:15:05:27'dt;
3 put now=;
4 put now= dbdate.;
5 call symputx('selldate',now);
6 RUN;
7 
8%put %sysfunc(putn(&selldate,dbdate.));
3 Code Block
PROC FORMAT
Explanation :
Definition of formats to display the full (%B) or abbreviated (%b) month name from a datetime value.
Copied!
1PROC FORMAT;
2 picture monthname
3 other = '%B ' (datatype=datetime);
4 picture monthabb
5 other = '%b ' (datatype=datetime);
6 RUN;
4 Code Block
DATA STEP Data
Explanation :
Test displaying formatted month names.
Copied!
1DATA _null_;
2 now = '11sep2010:15:05:27'dt;;
3 put now=;
4 put now= monthname.;
5 put now= monthname3.;
6 put now= monthabb.;
7 RUN;
5 Code Block
PROC FORMAT
Explanation :
Creation of a format with the (round) option to handle rounding of seconds during display.
Copied!
1PROC FORMAT;
2 picture myDayT (round)
3 low - high = '%0d%b%0Y:%0H:%0M:%0S'(datatype=datetime)
4 ;
5RUN;
6 Code Block
DATA STEP Data
Explanation :
Demonstration of the rounding effect on datetime values near the next second or next day.
Copied!
1DATA _null_;
2 datetime = '01apr2011:12:34:56.7'dt;
3 put datetime=myDayT.;
4 datetime = '01apr2011:23:59:59.7'dt;
5 put datetime=myDayT.;
6 RUN;
7 Code Block
PROC FORMAT
Explanation :
Usage of datatype=datetime_util to allow midnight to be displayed as '24:00:00' instead of '00:00:00' of the next day, useful in the utility industry.
Copied!
1PROC FORMAT;
2 picture abc (default=19)
3 other='%Y-%0m-%0d %0H:%0M:%0S' (datatype=datetime_util);
4 RUN;
8 Code Block
DATA STEP Data
Explanation :
Comparison of display for exact midnight and one second after midnight with the special format.
Copied!
1DATA _null_;
2 x = '01nov2008:00:00:00'dt; put x=abc.;
3 x = '01nov2008:00:00:01'dt; put x=abc.;
4 RUN;
9 Code Block
PROC FORMAT
Explanation :
Formatting a duration (difference between two dates) using the %n directive to count the total number of days.
Copied!
1PROC FORMAT;
2 picture durtest(default=27)
3 other='%n days %H hours %M minutes' (datatype=time);
4 RUN;
10 Code Block
DATA STEP Data
Explanation :
Calculation of a difference between two datetimes and display of the result as a duration (days, hours, minutes).
Copied!
1DATA _null_;
2 start = '01jan2010:12:34'dt;
3 END = '01feb2010:18:36'dt;
4 diff = END - start;
5 put diff=durtest.;
6 RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.