1
+ /************************************************************************************************
2
+ USING DATE DIRECTIVES ON THE PICTURE STATEMENT
3
+ This program creates formats to display dates in a customized way.
4
+ Keywords: PROC FORMAT, FORMAT, PICTURE, DATATYPE=
5
+ SAS Versions: SAS 9, SAS Viya
6
+ Documentation: https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=default&docsetId=proc&docsetTarget=p1xidhqypi0fnwn1if8opjpqpbmn.htm
7
+ 1. The picture statement can create formats that can be used to display dates. Start by using PROC FORMAT with a PICTURE
8
+ statement that has a datatype= option.
9
+ 2. The picture statement identifies the name of the new format. It then specifies the range of values to which a template
10
+ will be applied. Use the LOW and HIGH to keywords to indicate that the range includes everything from the lowest value
11
+ to the highest.
12
+ 3. It then specifies a template or pattern for the format. In this case we use date directives to define the template. In
13
+ quotes this template includes three date directives - %0d specifies that the day of the month be written out in one or two
14
+ digits, %b specifies that the month be written out in a three letter abbreviation, and %Y specifies that a 4-digit
15
+ year be written out. These elements are separated by the tilde character (~). The pattern closes with spaces to account for
16
+ the length of labels.
17
+ 4. Next, in parentheses, the datatype is specified. The datatype= option can specify that the format is for a date,
18
+ a time, or a datetime.
19
+ 5. In the PROC PRINT apply the format and see the results.
20
+ 6. Text can be combined with picture templates to create truly customized formats. Text be can be used for some levels of a
21
+ format and date directives for other levels.
22
+ 7. SAS date constants (as well as time constants and datetime constants) can be used to specify the beginning and end of
23
+ a date range.
24
+ 8. In using date directives remember that case can matter. A lower case b results in a 3-letter abbreviation for month, while
25
+ an upper case B spells out the whole month.
26
+ ************************************************************************************************/
27
+
28
+ proc format ;
29
+ picture /*1*/ hirefmt /*2*/ low- high = '%0d~%b~%Y ' /*3*/ (datatype= date ) /*4*/ ;
30
+ run ;
31
+
32
+ data crew ;
33
+ infile cards ;
34
+ input FirstName : $35 . LastName : $35 . HireDate : date9.;
35
+ cards ;
36
+ SALLY BEAUMONT 07NOV1992
37
+ CHRISTOPHER BERGAMASCO 12MAY1985
38
+ BARBARA- ANN BETHEA 04AUG1988
39
+ ROBERT BJURSTROM 22APR1987
40
+ SUSAN BONDS 23SEP1982
41
+ TERESA CHANG 03APR1994
42
+ JOANN CHOPRA 31MAR1993
43
+ ANNETTE CHRISTENSEN 12MAY1983
44
+ JOHN CHRISTIAN 15JAN1982
45
+ DOUGLAS CIAMPA 13JAN1984
46
+ JOHN CLAYTON 12OCT1986
47
+ WILLIAM DIELEMAN 26NOV1983
48
+ DANIEL DOWELL 09FEB1994
49
+ JOANNE DOWTY 05JUL1983
50
+ KENNETH EATON 05JUL1993
51
+ ANITA EHRISMAN 26JAN1984
52
+ KAREN ELAM 26FEB1986
53
+ GREGORY ELLIS 23FEB1981
54
+ ROBERT EUNICE 15APR1994
55
+ LISA FEENSTRA 04APR1993
56
+ ;
57
+ run ;
58
+
59
+ proc print data=crew; /*5*/
60
+ var firstname lastname hiredate;
61
+ format hiredate hirefmt.;
62
+ title 'Crew Hire Dates with Custom Date Format' ;
63
+ run ;
64
+
65
+ proc format ;
66
+ picture mrg /*6*/
67
+ low- <'01apr1990'd = 'Pre-Merger' /*7*/
68
+ '01apr1990'd - '31mar1993'd = '%B during Merger Process ' (datatype= date ) /*8*/
69
+ other = 'Post-Merger' ;
70
+ run ;
71
+ proc print data=crew(obs =20 );
72
+ var firstname lastname hiredate;
73
+ format hiredate mrg.;
74
+ title 'Crew Hire Dates with Text/Date Format' ;
75
+ run ;
0 commit comments