|
| 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; |
0 commit comments