BookmarkSubscribeRSS Feed
cas412
Calcite | Level 5

I'm trying to use a macro to generate a user-created format that translates numbers (1, 2, ...) into state names (Alabama, Alaska, ...).

I'm trying to do it using a do loop, assigning j values 1 through 51, and cycling it through the format statement. When I do that, it output the format after each state, so that the next state overwrites it and at the end I only have one format, 51 ="Wyoming". I've tried moving the do loop and %end all over, but can't figure it out. Here's what I have so far:

DM Log 'Clear';

DM Out 'Clear';

Options PS=100 Symbolgen Mprint;

Title;

/* MACRO DO_FORMAT STARTS HERE */

%macro do_format;

proc format;

Any advice or suggestions?

%do i=1 %to 51 ;

%let state=%scan(Alabama@Alaska@Arizona@Arkansas@California@Colorado@Connecticut@Delaware@District of Columbia@

          Florida@Georgia@Hawaii@Idaho@Illinois@Indiana@Iowa@Kansas@Kentucky@Louisiana@Maine@Maryland@Massachusetts@

          Michigan@Minnesota@Mississippi@Missouri@Montana@Nebraska@Nevada@New Hampshire@New Jersey@New Mexico@

          New York@North Carolina@North Dakota@Ohio@Oklahoma@Oregon@Pennsylvania@Rhode Island@South Carolina@

          South Dakota@Tennessee@Texas@Utah@Vermont@Virginia@Washington@West Virginia@Wisconsin@Wyoming, &i, @);

value _state &i  = "&state" %end;

run;

%end;

%mend do_format;

%do_format;

2 REPLIES 2
Reeza
Super User

use cntlin instead and create a dataset.

data want;

type="C";

fmtname="state_name";

do i=1 to 51 ;

label=compress(scan("Alabama@Alaska@Arizona@Arkansas@California@Colorado@Connecticut@Delaware@District of Columbia@Florida@Georgia@Hawaii@Idaho@Illinois@Indiana@Iowa@Kansas@Kentucky@Louisiana@Maine@Maryland@Massachusetts@Michigan@Minnesota@Mississippi@Missouri@Montana@Nebraska@Nevada@New Hampshire@New Jersey@New Mexico@New York@North Carolina@North Dakota@Ohio@Oklahoma@Oregon@Pennsylvania@Rhode Island@South Carolina@South Dakota@Tennessee@Texas@Utah@Vermont@Virginia@Washington@West Virginia@Wisconsin@Wyoming", i, "@"));

start=i;

output;

end;

run;

proc format cntlin=want;

run;

Tom
Super User Tom
Super User

You need to move the VALUE keyword before the %DO loop and the semi-colon that ends the statement after the %DO loop.

%macro do_format;

%local i states;

%let states

=Alabama@Alaska@Arizona@Arkansas@California@Colorado@Connecticut@Delaware

@District of Columbia@Florida@Georgia@Hawaii@Idaho@Illinois@Indiana@Iowa@Kansas

@Kentucky@Louisiana@Maine@Maryland@Massachusetts@Michigan@Minnesota@Mississippi

@Missouri@Montana@Nebraska@Nevada@New Hampshire@New Jersey@New Mexico

@New York@North Carolina@North Dakota@Ohio@Oklahoma@Oregon@Pennsylvania

@Rhode Island@South Carolina@South Dakota@Tennessee@Texas@Utah@Vermont

@Virginia@Washington@West Virginia@Wisconsin@Wyoming

;

proc format;

value _state

%do i=1 %to 51 ;

&i  = "%scan(&states,&i,@)"

%end;

;

run;

%end;

%mend do_format;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 1219 views
  • 0 likes
  • 3 in conversation