/* stcnvt: utility macro that can list and/or convert between US state (and state equivs. like Virgin Islands) FIPS and postal abbrevation codes. Single positional parm should be a valid code of either type. Macro "returns" the corresponding code in the other system. For example: retain fcode "%stcnvt(wi)"; *<--value of fcode is "55"; stab="%stcnvt(17)"; *<--value of stab is "il"; This works with macro variables, NOT data step variables. Use SAS format codes like $fipstab to do that kind of convert. Optional keyword parm can be used to generate a listing of all the codes. Specify list=1 or list=y to get this. NOTE: all stab abbreviations are assumed to be lower case. */ %macro stcnvt(ss, list=0); %let stabs1=al ak az ar ca co ct de dc fl ga hi id il in ia; %let stabs2=ks ky la me md ma mi mn ms mo mt ne nv nh nj nm ny; %let stabs3=nc nd oh ok or pa ri sc sd tn tx ut vt va wa wv; %let stabs4=wi wy as gu mp pw pr vi; %*<--incl territories; %let stabs = %quote(&stabs1 &stabs2 &stabs3 &stabs4); %let stfps1=01 02 04 05 06 08 09 10 11 12 13 15 16 17 18 19; %let stfps2=20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36; %let stfps3=37 38 39 40 41 42 44 45 46 47 48 49 50 51 53 54; %let stfps4=55 56 60 66 69 70 72 78; %let stfps = %quote(&stfps1 &stfps2 &stfps3 &stfps4); %if %upcase(&list) = Y or &list eq 1 %then %do; %*-- Option to list a table of codes: ; %put List option specified: FIPS state codes and abbreviations; %do ix = 1 %to 60; %let fips = %scan(&stfps, &ix); %if &fips eq %str() %then %goto endloop; %let stab = %scan(&stabs, &ix); %put &fips %str(=) &stab; %end; %endloop: %if &ss eq %then %goto endmac; %end; %local _fx _ax; %*-- Simple strategy: two long parallel code strings. Find the argument code in one and the "answer" is in the same relative position of the other. So just substr to return converted value. ; %let _ax = %index(&stabs, &ss); %if &_ax %then %do; %*-- Return corresponding FIPS code. ; %substr(&stfps, &_ax,2) %end; %else %do; %let _fx = %index(&stfps, &ss); %if &_fx %then %do; %*-- Return corresponding postal abbreviation code. ; %substr(&stabs,&_fx,2) %end; %else %do; %put *** Invalid argument "&ss" to STCNVT macro***; &ss %*-- return argument ; %end; %end; %endmac: %mend stcnvt;