I'm trying to work on an exercise to better train myself on Macro commands, one of them is to do a Do_Format macro that would resolve in the Formats for the states. I am do use a %Do loop that would iterate as often as state names are, and also use %Scan() function to select the state name corresponding to the iteration. I ran a couple of my own macros for Do_Format but i got the exact same out as the one this Proc statements gives. It is suppose to give me this output:
CREATE A NEW Do_Format Macro that uses a Do loop with Scan
Proc Format;
VALUE _STATE
1 = "Alabama"
2 = "Alaska"
3 = "Arizona"
4 = "Arkansas"
5 = "California"
6 = "Colorado"
7 = "Connecticut"
8 = "Delaware"
9 = "District of Columbia"
10 = "Florida"
11 = "Georgia"
12 = "Hawaii"
13 = "Idaho"
14 = "Illinois"
15 = "Indiana"
16 = "Iowa"
17 = "Kansas"
18 = "Kentucky"
19 = "Louisiana"
20 = "Maine"
21 = "Maryland"
22 = "Massachusetts"
23 = "Michigan"
24 = "Minnesota"
25 = "Mississippi"
26 = "Missouri"
27 = "Montana"
28 = "Nebraska"
29 = "Nevada"
30 = "New Hampshire"
31 = "New Jersey"
32 = "New Mexico"
33 = "New York"
34 = "North Carolina"
35 = "North Dakota"
36 = "Ohio"
37 = "Oklahoma"
38 = "Oregon"
39 = "Pennsylvania"
40 = "Rhode Island"
41 = "South Carolina"
42 = "South Dakota"
43 = "Tennessee"
44 = "Texas"
45 = "Utah"
46 = "Vermont"
47 = "Virginia"
48 = "Washington"
49 = "West Virginia"
50 = "Wisconsin"
51 = "Wyoming"
;
VALUE _CIMPAGE
0 = "0 Years old"
1 = "1 Year old"
2 = "2 Years old"
3 = "3 Years old"
4 = "4 Years old"
5 = "5 Years old"
6 = "6 Years old"
7 = "7 Years old"
8 = "8 Years old"
9 = "9 Years old"
10 = "10 Years old"
11 = "11 Years old"
12 = "12 Years old"
13 = "13 Years old"
14 = "14 Years old"
15 = "15 Years old"
16 = "16 Years old"
17 = "17 Years old"
;
Run;
Is there a question there somewhere?
Are you asking how to take a list of items and use it to generate a numbered list like you might have in a FORMAT?
%macro xx(list,dlm=|);
%local i ;
%do i=1 %to %sysfunc(countw(&list,&dlm));
&i = %sysfunc(quote(%qscan(&list,&i,&dlm)))
%end;
%mend xx;
proc format ;
value states %xx(Alabama|Alaska|Arizona|Arkansas|...) ;
run;
Is there a question there somewhere?
Are you asking how to take a list of items and use it to generate a numbered list like you might have in a FORMAT?
%macro xx(list,dlm=|);
%local i ;
%do i=1 %to %sysfunc(countw(&list,&dlm));
&i = %sysfunc(quote(%qscan(&list,&i,&dlm)))
%end;
%mend xx;
proc format ;
value states %xx(Alabama|Alaska|Arizona|Arkansas|...) ;
run;
Tom,
The questions was where do I use Scan within the the new macro Do_Format, that would resolve in the Formats for the states. I am to use a %Do loop that would iterate as often as state names are, and also use %Scan() function to select the state name corresponding to the iteration. I ran a couple of my own macros for Do_Format but i got the exact same out as the one this Proc statements gives. It is suppose to give me this output:
What can I create as a MACRO to use the PROC statement to give this output above?
Tom has already given you the complete solution, just copy/paste it and expand the states list.
examine the functions
fipname
fipnamel
fipstate
which convert the Federal Information Processing integers for each state
to corresponding name (upcase), name-lowcase and two-letter postal code
see also
stfips
stname
stnamel
check this trick, which is known as an associative array
%let n = 37;
%let st37 = NC;
%let state37 = North Carolina;
%put st&n=&&st&n;
%put state&n=&&state&n;
%let NC = North Carolina;
%put st&n=&&&&&&st&n;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.