Dear SAS Community,
I am trying to use a macro within an array statement. The first set of commands worked, but the second didn't. I'd appreciate your help.
Thanks,
Brent Fulton
UC Berkeley
THIS WORKED
data new;
set old;
array a {*} $ proc1 proc2 proc3;
array b {*} $ proc1b proc2b proc3b;
do j=1 to dim(a);
b{j} = substr(a{j},1,1);
end;
run;
THIS DIDN'T WORK (it is same as above, except use macro proclist. error was "Expecting a variable length specification.")
%let proclist='proc1 proc2 proc3';
data new;
set old;
array a {*} $ &proclist;
array b {*} $ proc1b proc2b proc3b;
do j=1 to dim(a);
b{j} = substr(a{j},1,1);
end;
run;
Hi There,
In your %let statement remove the single quotes then it will work.
%let proclist=proc1 proc2 proc3;
The reason being is because the macro facility is text and as such you don't need to quote your macro variable values.
Cheers,
Michelle
Hi There,
In your %let statement remove the single quotes then it will work.
%let proclist=proc1 proc2 proc3;
The reason being is because the macro facility is text and as such you don't need to quote your macro variable values.
Cheers,
Michelle
Thank you, Michelle, that worked.
Btw, is there a way to have a macro contain a numeric?
And as follow-up to arrays, I am trying to do the following: (1) change missings to 'zzzzz', and then add leading zeros e.g., make '123' be '00123'. All variables are character length 5. However, I get an invalid argument to the input function below. When I don't use arrays, it works fine. I'd appreciate your advice.
Last, I'm running out of disk space. Is there a way to do this without creating two additional sets of variables (i.e., proclistb and proclistc)?
Thanks,
Brent
%let proclist proc1 proc2 proc3;
%let proclistb=proc1b proc2b proc3b;
%let proclistc=proc1c proc2c proc3c;
run;
*change missings to zzzzz, add leading zeros;
data new;
set old;
array a {*} $5 &proclist;
array b {*} $5 &proclistb;
array c {*} $5 &proclistc;
do j=1 to dim(a);
if a{j}~=' ' then b{j} = a{j}; else b{j}=zzzzz; /* do i need to have it be 'zzzzz' ?*/
c{j} = put(input(b{j},best.),z5.);
end;
run;
You need enclose string literals inside of quotes. Without the quotes SAS will look for a variable with the name zzzzz.
There you are not dealing with macro code at all, just normal SAS syntax.
Thank you, Tom. I switched the line in question above to:
if a{j}~=' ' then b{j} = a{j}; else b{j}='zzzzz';
However, I still get an invalid argument error to the input function on the next line:
c{j} = put(input(b{j},best.),z5.);
Do you or others have suggestions?
thanks,
Brent
Hi ... if it's just the PROC variables you want to edit , you can skip the macro variable (yes/no?).
If you just want to vary the number of PROCs, use that number as the macro variable.
data old;
input (proc1 proc2 proc3) (: $5.);
datalines;
12345 . 999
88 77 .
;
run;
%let n=3;
*change missings to zzzzz, add leading zeros;
data nw;
set old;
array proc{&n};
do j=1 to &n;
proc{j} = ifc(^missing(proc(j)), translate(right(proc(j)),'0',' '), 'zzzzz');
end;
drop j;
run;
proc1 proc2 proc3
12345 zzzzz 00999
00088 00077 zzzzz
this worked well, Mike, thank you.
best,
Brent
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.