DATA Step, Macro, Functions and more

Need help with creating a do loop

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Need help with creating a do loop

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.


Accepted Solutions
Solution
‎03-19-2012 04:55 PM
Respected Advisor
Posts: 4,925

Need help with creating a do loop

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


All Replies
Respected Advisor
Posts: 4,925

Need help with creating a do loop

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

PG
Occasional Contributor
Posts: 5

Need help with creating a do loop

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.

Solution
‎03-19-2012 04:55 PM
Respected Advisor
Posts: 4,925

Need help with creating a do loop

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
Occasional Contributor
Posts: 5

Need help with creating a do loop

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.

Respected Advisor
Posts: 4,925

Need help with creating a do loop

%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
Occasional Contributor
Posts: 5

Need help with creating a do loop

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

Respected Advisor
Posts: 4,925

Need help with creating a do loop

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

PG
Occasional Contributor
Posts: 5

Need help with creating a do loop

Sorry for my ignorance, and thank you again!

Respected Advisor
Posts: 4,925

Need help with creating a do loop

You're welcome. Cheers!

PG

PG
🔒 This topic is solved and locked.

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

Discussion stats
  • 9 replies
  • 237 views
  • 0 likes
  • 2 in conversation