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.
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
Is this a homework? What have you tried so far?
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.
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
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.
%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?
I just ran your macro without making any changes. But if you open the dataset, the value inside is C and not CA
It just looks that way in FSVIEW. Expand the column width and you will see that the whole value (2-letter) is there.
Sorry for my ignorance, and thank you again!
You're welcome. Cheers!
PG
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.