Question

I have the following code

%macro test;
data ttest;
set test;
%do 
var14_t_0=lag(var14);
%do i=1 %to 6;
var14_t_&i=lag(var14_t_%eval(&i-1));
%end;
drop var14;
run;
%mend;
%test;

the var14 is a variable name with which, I'm using the lag function. However, I have a lot of variables in my table. I'm trying to automate the variable naming but I'm sure how to do this.

I thought about the following:

Use proc sql to create a table like below:

proc sql;
create table label as 
select name
from dictionary.columns
where memname = upcase('Afr') and name not in ('date');

Once the table created, I would only need to take the variable name from inside the label table. But the issue is I'm not sure how to link the code above with my table.

I'm not saying I need the table and I may need something else but I'm unsure of the steps as I'm new to SAS.

Thanks in advance for any tips you can provide.

Was it helpful?

Solution

You're almost there.

proc sql;
select name into :namelist separated by ' '
from ... (as you have above)
;
quit;

That generates a macro variable, &namelist, that has all of the names in one list separated by space. You can then use it however you wish. You can alter the separated by character (s) of course, and you can select complex expressions (ie, instead of select name, you can say select cats('%mymacro(mvar=',name,')') or similar.

I recommend trying to do the latter - constructing a macro that will do whatever it is you want done multiple times, then using this method to construct calls to that macro. It's usually easier than macro looping over a list of names.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top