%let vars = a b c;
data want;
set have;
array have {*} &vars.;
array ret {%sysfunc(countw(&vars.))} _temporary_;
do i = 1 to dim(have);
if have{i} ne .
then ret{i} = have{i};
else have{i} = ret{i};
end;
drop i;
run;
Untested, posted from my tablet. If it works, all you have to do is to populate the macro variable.
What does the macro do? It might be that you don't have to call the macro repeatedly, but can use a data step do loop over an array, or similar.
That can be done in a single data step, using an array for the variables and a temporary array of same size for the retained values.
All you need to create first is a macro variable with the variable names, for the array definitions; the size of the temporary array can be determined from that with %sysfunc(countw()).
Show the code you run for a single variable, and we'll show you how to expand it.
%let vars = a b c;
data want;
set have;
array have {*} &vars.;
array ret {%sysfunc(countw(&vars.))} _temporary_;
do i = 1 to dim(have);
if have{i} ne .
then ret{i} = have{i};
else have{i} = ret{i};
end;
drop i;
run;
Untested, posted from my tablet. If it works, all you have to do is to populate the macro variable.
@someone_new wrote:
How about range of columns instead of %let vars method?
If you want to use a variable list (var1-var10 for example) instead of naming each variable then you need to modify the data step to just make the temporary array large enough for any possible real use.
%let vars = a b var1-var20 firstvar-numeric-lastvar;
...
array have {*} &vars.;
array ret {1000} _temporary_;
You need to know the number of variables before you enter the data step, to correctly define the temporary array. So you will need to determine the list of variables, anyway.
@someone_new wrote:
How about range of columns instead of %let vars method?
My inner old-school coder simply does not want to waste memory 😉
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.