BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
amitsas
Calcite | Level 5

Hi,

I am hoping someone can help with request.

I need to create (say) 5 tables or data sets, for 5 states - CA, VA, NY, TX and OK

Each table has some information based on some criteria (say, name of state and its population). So the table for California looks like

CA     34,000,000

I can have a data step to do this, but I want to put the names of states in a marco variable string along with a do loop so that the do loop runs and creates a data step for each state in the macro variable string,

for example

%LET STATE = CA VA NY TX OK;

do loop (CA)

followed by

do loop VA etc..

Thanks to all who try to help.

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

What you want would like this, I guess :

options mprint;

%LET STATES = CA VA NY TX OK;

%macro doloop(values);
%let i = 1;
%do %while(%length(%scan(&values,&i)) > 0);
  %let value=%scan(&values,&i);
  data _&value.;
   t = "&value";
  run;
  %let i = %eval(&i+1);
%end;
%mend;

%doLoop(&STATES);

PG

PG

View solution in original post

9 REPLIES 9
PGStats
Opal | Level 21

Is this a homework? What have you tried so far?

PG
amitsas
Calcite | Level 5

This is not a homework.

I haven't used SAS in a while. I have a list of close to 2000 values for which I have retrieve some data. And I do not want to run the code 2000 times, based on each value.

From what i remember when I did do it, I used a long character string as a macro variable, and resolved the value by moving the do loop counter by 1 and then ran the code.

PGStats
Opal | Level 21

What you want would like this, I guess :

options mprint;

%LET STATES = CA VA NY TX OK;

%macro doloop(values);
%let i = 1;
%do %while(%length(%scan(&values,&i)) > 0);
  %let value=%scan(&values,&i);
  data _&value.;
   t = "&value";
  run;
  %let i = %eval(&i+1);
%end;
%mend;

%doLoop(&STATES);

PG

PG
amitsas
Calcite | Level 5

Thanks a lot! This works. This is very similar to how I did it originally.

The only change needed is probably in the %scan function. Right now it is only using the C from CA (and N from NY etc) fwhen resolving

%let value=%scan(&values,&i);,

while it needs to use the whole CA.I can probably take care of that.

PGStats
Opal | Level 21

%SCAN is supposed to extract a word, not just a letter. In my tests, 5 datasets were created : _CA, _VA, _NY, _TX, and _OK. Can you post your macro?

PG
amitsas
Calcite | Level 5

I just ran your macro without making any changes. But if you open the dataset, the value inside is C and not CA

PGStats
Opal | Level 21

It just looks that way in FSVIEW. Expand the column width and you will see that the whole value (2-letter) is there.

PG
amitsas
Calcite | Level 5

Sorry for my ignorance, and thank you again!

PGStats
Opal | Level 21

You're welcome. Cheers!

PG

PG

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
  • 9 replies
  • 1109 views
  • 0 likes
  • 2 in conversation