DATA Step, Macro, Functions and more

A do_format macro with %Scan, assistance needed

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

A do_format macro with %Scan, assistance needed

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:

states.png

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;


Accepted Solutions
Solution
‎06-04-2015 05:07 PM
Super User
Super User
Posts: 6,502

Re: A do_format macro with %Scan, assistance needed

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;

View solution in original post


All Replies
Solution
‎06-04-2015 05:07 PM
Super User
Super User
Posts: 6,502

Re: A do_format macro with %Scan, assistance needed

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;

Occasional Contributor
Posts: 5

Re: A do_format macro with %Scan, assistance needed

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:

states.png

What can I create as a MACRO to use the PROC statement to give this output above?

Super User
Posts: 6,972

Re: A do_format macro with %Scan, assistance needed

Tom has already given you the complete solution, just copy/paste it and expand the states list.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Regular Contributor
Posts: 203

Re: A do_format macro with %Scan, assistance needed

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;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 236 views
  • 4 likes
  • 4 in conversation