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!
title1 '12.2.1 Picture Formats';
title2 'Using Date Directives';
proc format;
picture dbdate
other = '%Y-%0m-%0d:%0H:%0M:%0S' (datatype=datetime);
run;
1
title1 '12.2.1 Picture Formats';
2
title2 'Using Date Directives';
3
PROC 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!
data _null_;
now = '11sep2010:15:05:27'dt;
put now=;
put now= dbdate.;
call symputx('selldate',now);
run;
%put %sysfunc(putn(&selldate,dbdate.));
1
DATA _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!
proc format;
picture monthname
other = '%B ' (datatype=datetime);
picture monthabb
other = '%b ' (datatype=datetime);
run;
1
PROC 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!
data _null_;
now = '11sep2010:15:05:27'dt;;
put now=;
put now= monthname.;
put now= monthname3.;
put now= monthabb.;
run;
1
DATA _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.
low - high = '%0d%b%0Y:%0H:%0M:%0S'(datatype=datetime)
4
;
5
RUN;
6 Code Block
DATA STEP Data
Explanation : Demonstration of the rounding effect on datetime values near the next second or next day.
Copied!
data _null_;
datetime = '01apr2011:12:34:56.7'dt;
put datetime=myDayT.;
datetime = '01apr2011:23:59:59.7'dt;
put datetime=myDayT.;
run;
1
DATA _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.
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!
data _null_;
start = '01jan2010:12:34'dt;
end = '01feb2010:18:36'dt;
diff = end - start;
put diff=durtest.;
run;
1
DATA _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.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.