Solved
Contributor
Posts: 27

# Do loops

Hi all,

I would like to have something like this:

do i=1 to 12;

month&i = 5 + i - 1;

output;

end;

which should make the variables

month1 = 5

month2 = 6

...

month12 = 16

However, writing month&i doesn't work.

Any ideas?

Marco

Accepted Solutions
Solution
‎05-07-2014 04:48 AM
Super User
Posts: 10,555

## Re: Do loops

Try this:

data want;

.....

array months{*} month1 - month12;

do i = 1 to 12;

months{i} = 5 + i - 1;

end;

...

drop i;

run;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

All Replies
Super User
Posts: 9,828

## Re: Do loops

Hi,

The & (ampersand) is for macro processing.  The easiest way to change it to use & is to wrap a macro around it:

%tmp ();

data a;

%do I=1 %to 10;

b=&I.;

%end;

run;

%mend tmp()

%tmp;

However I am not saying that is the best solution or the way to go, only answering the specific item.  If I wanted to do this type of thing then I would generate the code:

data _null_;

call execute('data want;');

do i=1 to 12;

call execute('month'||strip(put(I,best.))||'=5+'||strip(put(I,best.))||'-1;

output;');

end;

call execute('run;');

run;

This will generate a dataset with the 12 month statements and execute it.

Solution
‎05-07-2014 04:48 AM
Super User
Posts: 10,555

## Re: Do loops

Try this:

data want;

.....

array months{*} month1 - month12;

do i = 1 to 12;

months{i} = 5 + i - 1;

end;

...

drop i;

run;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 27

## Re: Do loops

Thanks, that works.

Now I would like to make variables month1 - month12 available for use later in the code. I've come up with this, which works:

call symputx('month1',month1);

call symputx('month2',month2);

...

call symputx('month12',month12);

But is there any way to put that as a loop?

Thanks

Super User
Posts: 9,828

## Re: Do loops

Yes, with KurtBremsers code you can just put the call symputx after the months{I} statement.  However, this goes back to my post and generating the code.  Why go to all the bother of creating macro varibles when you can just expand the code generator to generate the later code you also need, e.g. if I want to create a dataset with the month name:

data _null_;

do i=1 to 12;

call execute('data month'||strip(put(5+I,best.))||';

do some other code;

run;');

end;

run;

You really see the benefits on large programs, when all the generator needs is some parameterization.

Contributor
Posts: 27

## Re: Do loops

call symputx('months(i)',months(i);

?

Ok, I'll look into the call execute function, thanks again.

Super User
Posts: 9,828

## Re: Do loops

Been a while since I use symput but:

call symput('months'||strip(put(I,best.)),months{I});

You need to use curly braces for arrays.  I hope the concatenation works in symput like that :smileyshocked

Super User
Posts: 10,555

## Re: Do loops

call symput('month'!!strip(put(i,best.)),strip(put(months{i},best.)));

in the loop.

I just added some small things to RW9's suggestion to avoid NOTE's in the log and any unwanted blanks in the macro vars.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
🔒 This topic is solved and locked.