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?
Thanks in advance,
Marco
Try this:
data want;
.....
array months{*} month1 - month12;
do i = 1 to 12;
months{i} = 5 + i - 1;
end;
...
drop i;
run;
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.
Try this:
data want;
.....
array months{*} month1 - month12;
do i = 1 to 12;
months{i} = 5 + i - 1;
end;
...
drop i;
run;
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
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.
How, just by adding
call symputx('months(i)',months(i);
?
Ok, I'll look into the call execute function, thanks again.
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:)
Just add
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.
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.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.