Skip to content

Commit fa89f54

Browse files
authored
Merge pull request #37 from ChrisRiddiough/main
Update Using Functions to create formats.
2 parents 9465bb8 + f6e6520 commit fa89f54

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/************************************************************************************************
2+
USING A FORMAT TO CREATE A FUNCTION
3+
This program creates a format based on other formats for different ranges of data.
4+
Keywords: PROC FORMAT, FORMAT, VALUE, PROC FCMP
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 format to display Australian postal codes as state codes.
8+
2. Use ranges with character values to define the format.
9+
3. In PROC FCMP define a function that will apply the format to Australian codes and uses
10+
the ZIPSTATE function for US States.
11+
4. Use PROC REPORT to display the results of the new function.
12+
************************************************************************************************/
13+
14+
proc format;/*1*/
15+
value $postabb '1000'-'1999',
16+
'2000'-'2599',
17+
'2619'-'2898',
18+
'2921'-'2999'='NSW'
19+
'0200'-'0299',
20+
'2600'-'2618',
21+
'2900'-'2920'='ACT'
22+
'3000'-'3999',
23+
'8000'-'8999'='VIC'
24+
'4000'-'4999',
25+
'9000'-'9999'='QLD'
26+
'5000'-'5799',
27+
'5800'-'5999'='SA'
28+
'6000'-'6797',
29+
'6800'-'6999'='WA'
30+
'7000'-'7799',
31+
'7800'-'7999'='TAS'
32+
'0800'-'0899',
33+
'0900'-'0999'='NT';/*2*/
34+
run;
35+
36+
title;
37+
38+
proc fcmp outlib=work.functions.char; /*3*/
39+
function StateProv(Country $,Code $) $ 5;
40+
if upcase(country) ='AU' then stpr=put(code,$postabb.);
41+
else if upcase(country)='US' then stpr=zipstate(code);
42+
else stpr=' ';
43+
return(stpr);
44+
endsub;
45+
quit;
46+
47+
options cmplib=work.functions;
48+
49+
data addresses;
50+
infile cards dlm=',' dsd;
51+
input EmployeeID : $10. EmployeeName : $30. City : $15. PostalCode : $5. Country : $2.;
52+
cards;
53+
121044,"Abbott, Ray","Miami-Dade",33135,"US"
54+
120145,"Aisbitt, Sandy","Melbourne",8001,"AU"
55+
120761,"Akinfolarin, Tameaka","Philadelphia",19145,"US"
56+
120656,"Amos, Salley","San Diego",92116,"US"
57+
121107,"Anger, Rose","Philadelphia",19142,"US"
58+
121038,"Anstey, David","Miami-Dade",33157,"US"
59+
120273,"Antonini, Doris","Miami-Dade",33141,"US"
60+
120759,"Apr, Nishan","San Diego",92071,"US"
61+
120798,"Ardskin, Elizabeth","Miami-Dade",33177,"US"
62+
121030,"Areu, Jeryl","Miami-Dade",33133,"US"
63+
121017,"Arizmendi, Gilbert","San Diego",91950,"US"
64+
121062,"Armant, Debra","San Diego",92025,"US"
65+
121119,"Armogida, Bruce","Philadelphia",19119,"US"
66+
120812,"Arruza, Fauver","Miami-Dade",33133,"US"
67+
120756,"Asta, Wendy","Philadelphia",19145,"US"
68+
120754,"Atkins, John","Miami-Dade",33161,"US"
69+
120185,"Bahlman, Sharon","Sydney",2165,"AU"
70+
120109,"Baker, Gabriele","Sydney",2119,"AU"
71+
120710,"Baltzell, Timothy","Philadelphia",19140,"US"
72+
121007,"Banaszak, John","Philadelphia",19139,"US"
73+
121011,"Banchi, Steven","Miami-Dade",33031,"US"
74+
120188,"Baran, Shanmuganathan","Sydney",1225,"AU"
75+
120144,"Barbis, Viney","Sydney",2114,"AU"
76+
120168,"Barcoe, Selina","Melbourne",8003,"AU"
77+
120182,"Barreto, Geok-Seng","Sydney",1115,"AU"
78+
;
79+
run;
80+
81+
proc sort data= addresses;
82+
by country;
83+
run;
84+
85+
proc report data=addresses nowd;/*4*/
86+
columns Country City PostalCode State EmployeeID EmployeeName;
87+
by country;
88+
define State / computed 'State/Province';
89+
compute State / char length=5;
90+
State=StateProv(Country,PostalCode);
91+
endcomp;
92+
title 'Employees with State Codes';
93+
run;
94+
95+
title;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/************************************************************************************************
2+
USING FUNCTIONS TO CREATE FORMATS
3+
This program creates a FORMAT based on functions created by PROC FCMP.
4+
Keywords: PROC FORMAT, PROC FCMP, 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. Use PROC FCMP to create two new functions, START and END.
8+
2. The START function will take a date as its argument and from that date will go to the beginning of the week
9+
and then go forward to the next Monday (day 2 of the week.)
10+
3. The END function will go to the end of the next week, a Saturday. One is subtracted from the result so that the
11+
End function returns a Friday.
12+
4. The options statement tells SAS where to look for the new functions.
13+
5. The VALUE statements in PROC FORMAT first identifies the name of the new format. The keyword OTHER tells SAS
14+
to apply the format to all nonmissing values.
15+
6. The square brackets surround the function to be used to define the format. In the start format the START function
16+
is used so that any date will be labeled as the Monday of the week of the date.
17+
7. Similarly the end format uses the END function to label the date as the next Friday.
18+
8. Create data that includes dates to which the formats can be applied.
19+
9. In the PROC REPORT apply the format to see the results.
20+
************************************************************************************************/
21+
22+
proc fcmp outlib=work.functions.DateType;/*1*/
23+
function START(Date) $ 9;/*2*/
24+
dt=intnx('week.2', Date, 0);/*3*/
25+
return(put(dt,date9.));
26+
endsub;
27+
function END(Date) $ 9;
28+
dt=intnx('week.7', Date, 1) - 1;
29+
return(put(dt,date9.));
30+
endsub;
31+
quit;
32+
33+
options cmplib=work.functions;/*4*/
34+
35+
proc format;/*5*/
36+
value start other=[start()];/*6*/
37+
value end other=[end()]; /*7*/
38+
run;
39+
40+
data orders;/*8*/
41+
infile cards;
42+
input CustomerID DeliveryDate : date9. OrderID;
43+
cards;
44+
12 11MAR2010 1238678581
45+
12 23JUN2011 1242610991
46+
13 28JUL2010 1239744161
47+
16 20JUL2010 1239713046
48+
16 02OCT2010 1240314956
49+
18 17FEB2011 1241461856
50+
19 06FEB2010 1238370259
51+
20 08NOV2010 1240613362
52+
24 14JUL2011 1242773202
53+
24 21APR2010 1238968334
54+
;
55+
run;
56+
57+
proc report data=orders nowd;/*9*/
58+
columns CustomerID OrderID DeliveryDate ('Expected Delivery Between' DeliveryDate=DD1 DeliveryDate=DD2);
59+
define CustomerID / order 'Customer ID/' ;
60+
define OrderID / 'Order ID/' format=10.;
61+
define DeliveryDate / display 'Actual Delivery' format=weekdate.;
62+
define DD1 / display 'Start' format=start12.;
63+
define DD2 / display 'End' format=end12.;
64+
title 'Expected Delivery for Current Orders';
65+
run;
66+
67+
title;

0 commit comments

Comments
 (0)