Skip to content

Commit d49dbac

Browse files
Example of groupformat option on the by statement in a data step.
1 parent fbd799e commit d49dbac

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
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)