Skip to content

Commit 9465bb8

Browse files
authored
Merge pull request #36 from ChrisRiddiough/main
Add nested formats
2 parents c120f6a + 6a13757 commit 9465bb8

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/************************************************************************************************
2+
USING NESTED FORMATS
3+
This program creates a format based on other formats for different ranges of data.
4+
Keywords: PROC FORMAT, FORMAT, VALUE
5+
SAS Versions: SAS 9, SAS Viya
6+
Documentation: https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=default&docsetId=proc&docsetTarget=p1xidhqypi0fnwn1if8opjpqpbmn.htm
7+
1. Create a data set on which to test the new format.
8+
2. Use PROC FORMAT with a value statement. This identifies the name of the new format. Provide ranges for which you
9+
want to assign different formats.
10+
3. To nest formats use square brackets to identify the format to be applied to each range.
11+
4. In the PROC PRINT apply the format to see the results.
12+
************************************************************************************************/
13+
data payroll; /*1*/
14+
infile cards;
15+
input EmployeeID EmployeeGender : $1. Salary BirthDate : date9. HireDate : date9.;
16+
cards;
17+
120101 M 163040 18AUG1980 01JUL2007
18+
120102 M 108255 11AUG1973 01JUN1993
19+
120103 M 87975 22JAN1953 01JAN1978
20+
120104 F 46230 11MAY1958 01JAN1985
21+
120105 F 27110 21DEC1978 01MAY2003
22+
120106 M 26960 23DEC1948 01JAN1978
23+
120107 F 30475 21JAN1953 01FEB1978
24+
120108 F 27660 23FEB1988 01AUG2010
25+
120109 F 26495 15DEC1990 01OCT2010
26+
120110 M 28615 20NOV1953 01NOV1983
27+
;
28+
run;
29+
30+
proc format; /*2*/
31+
value benefit low-'31dec60'd=[weekdate50.] /*3*/
32+
'01jan61'd-'31dec93'd=[worddate20.]
33+
'01jan94'd-high='** Not Eligible **'
34+
;
35+
run;
36+
37+
title "Display Dates using a Nested Custom Format";
38+
proc print data=payroll;/*4*/
39+
format Birthdate Hiredate benefit.;
40+
run;
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/************************************************************************************************
2+
USING THE BY GROUPFORMAT STATEMENT TO PROCESS DATA
3+
This program creates a format that can be used to group data. It shows the use of the groupformat option in the data step.
4+
Keywords: PROC FORMAT, FORMAT, VALUE, GROUPFORMAT
5+
SAS Versions: SAS 9, SAS Viya
6+
Documentation: https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=default&docsetId=proc&docsetTarget=p1xidhqypi0fnwn1if8opjpqpbmn.htm
7+
1. Create a data set on which to test the new format. The data is sorted by the value to be formatted.
8+
2. Use PROC FORMAT with a value statement. Provide ranges for which you want to assign different grouping levels.
9+
3. Use the groupformat option on the by statement to allow the data step to read the data by groups.
10+
4. Because the data is being read with a by statement the first. and last. variables are created. In this example, the
11+
first.salary will be the lowest salary observation for a group and the last.salary observation will be the highest.
12+
5. In the PROC REPORT apply the format to see the results.
13+
************************************************************************************************/
14+
data payroll; /*1*/
15+
infile cards;
16+
input EmployeeID Salary BirthDate : date9. HireDate : date9.;
17+
cards;
18+
120185 25080 07APR1978 01DEC2010
19+
121101 25390 28AUG1990 01NOV2010
20+
121092 25680 08MAR1978 01AUG2006
21+
121106 25880 02FEB1973 01FEB2000
22+
121072 26105 10JAN1983 01SEP2008
23+
120178 26165 23NOV1958 01APR1978
24+
121027 26165 05MAY1968 01DEC1993
25+
121096 26335 18MAY1973 01MAY1999
26+
120112 26550 17FEB1973 01JUL1994
27+
121002 26650 18SEP1958 01DEC1983
28+
120687 26800 25MAY1983 01AUG2008
29+
120183 26910 10SEP1973 01DEC2010
30+
120999 27215 28DEC1963 01APR1988
31+
120153 27260 07MAY1983 01JAN2002
32+
121059 27425 25OCT1963 01APR1985
33+
120164 27450 26NOV1963 01FEB1986
34+
120120 27645 05MAY1948 01JAN1978
35+
121139 27700 19AUG1963 01JUL1991
36+
121075 28395 23DEC1948 01JAN1978
37+
120715 28535 12JUN1983 01FEB2009
38+
121042 28845 04APR1983 01NOV2003
39+
120799 29070 23MAR1983 01NOV2002
40+
121105 29545 09MAY1983 01JAN2007
41+
121061 29815 16JUL1958 01JUL1988
42+
121062 30305 28OCT1988 01AUG2010
43+
121019 31320 25JUN1990 01JUN2008
44+
121032 31335 24FEB1992 01MAR2010
45+
121080 32235 24JAN1963 01SEP1991
46+
121099 32725 19MAR1983 01MAY2004
47+
120787 34000 22AUG1973 01JAN2000
48+
120731 34150 15MAR1963 01SEP1987
49+
120754 34760 02JUN1992 01MAY2010
50+
120683 36315 12NOV1958 01JAN1978
51+
120757 38545 18MAR1948 01JAN1978
52+
120789 39330 14JUL1968 01DEC1986
53+
120774 45155 17SEP1982 01MAR2006
54+
120796 47030 12MAY1958 01MAR1987
55+
120793 47155 08AUG1973 01JUN2000
56+
120668 47785 23OCT1953 01DEC1982
57+
120813 50865 14SEP1973 01JAN1997
58+
120804 55400 11FEB1948 01JAN1978
59+
120712 63640 12JUN1953 01JAN1978
60+
121145 84260 22NOV1953 01APR1980
61+
120260 207885 02DEC1968 01NOV1988
62+
120259 433800 25JAN1968 01SEP1993
63+
;
64+
run;
65+
66+
proc format; /*2*/
67+
value salgrp low-<30000='Under $30,000'
68+
30000-<35000='$30,000 to $35,000'
69+
35000-<50000='$35,000 to $50,000'
70+
50000-high='Over $50,000';
71+
run;
72+
73+
data highlowsal;
74+
set payroll;
75+
by groupformat salary; /*3*/
76+
format salary salgrp.;
77+
if first.salary then output; /*4*/
78+
if last.salary then output;
79+
run;
80+
81+
proc report data=highlowsal nowd; /*5*/
82+
columns EmployeeID Salary Salary=SalGrp HireDate;
83+
define salary / display format=dollar12.;
84+
define hiredate / format=date9.;
85+
define salgrp / format=salgrp. 'Salary Group';
86+
title 'Lowest and Highest Salary by Salary Group';
87+
run;
88+
89+
title;

0 commit comments

Comments
 (0)