I have a list of 28 variable names I'd like to convert to x1-x28 in order to use a macro do loop for running proc genmod.
I've tried using an array as follows to assign values to x1-x28, but this doesn't work:
data macrovars (keep=y x1-x28);
set health_data;
array x[28] pre_CHF pre_VALVE pre_PULMCIRC pre_PERIVASC pre_PARA pre_NEURO pre_CHRNLUNG pre_DM pre_DMCX pre_HTN_C
pre_HYPOTHY pre_RENLFAIL pre_LIVER pre_ULCER pre_AIDS pre_LYMPH pre_METS pre_TUMOR pre_ARTH pre_COAG pre_OBESE
pre_WGHTLOSS pre_BLDLOSS pre_ANEMDEF pre_ALCOHOL pre_DRUG pre_PSYCH pre_DEPRESS;
output x1-x28;
run;
Is there a way to do this using arrays?
If you use an array then you have to create the new variables. I'm assuming here the variables are numeric. If not the code will need to be adjusted.
data macrovars (keep=y x1-x28);
set health_data;
array y[28] pre_CHF pre_VALVE pre_PULMCIRC pre_PERIVASC pre_PARA pre_NEURO pre_CHRNLUNG pre_DM pre_DMCX pre_HTN_C
pre_HYPOTHY pre_RENLFAIL pre_LIVER pre_ULCER pre_AIDS pre_LYMPH pre_METS pre_TUMOR pre_ARTH pre_COAG pre_OBESE
pre_WGHTLOSS pre_BLDLOSS pre_ANEMDEF pre_ALCOHOL pre_DRUG pre_PSYCH pre_DEPRESS;
array x [28] x1 - x28;
do i = 1 to dim(x);
x(i) = y(i);
end;
run;
If you use an array then you have to create the new variables. I'm assuming here the variables are numeric. If not the code will need to be adjusted.
data macrovars (keep=y x1-x28);
set health_data;
array y[28] pre_CHF pre_VALVE pre_PULMCIRC pre_PERIVASC pre_PARA pre_NEURO pre_CHRNLUNG pre_DM pre_DMCX pre_HTN_C
pre_HYPOTHY pre_RENLFAIL pre_LIVER pre_ULCER pre_AIDS pre_LYMPH pre_METS pre_TUMOR pre_ARTH pre_COAG pre_OBESE
pre_WGHTLOSS pre_BLDLOSS pre_ANEMDEF pre_ALCOHOL pre_DRUG pre_PSYCH pre_DEPRESS;
array x [28] x1 - x28;
do i = 1 to dim(x);
x(i) = y(i);
end;
run;
You cannot use an array to RENAME variables. You need to use the RENAME statement or RENAME= dataset option.
There is no need to use names like X1-X28 just because you want to use a loop in macro code.
Example macro code:
%let names=pre_CHF pre_VALVE pre_PULMCIRC pre_PERIVASC pre_PARA pre_NEURO pre_CHRNLUNG pre_DM pre_DMCX pre_HTN_C
pre_HYPOTHY pre_RENLFAIL pre_LIVER pre_ULCER pre_AIDS pre_LYMPH pre_METS pre_TUMOR pre_ARTH pre_COAG pre_OBESE
pre_WGHTLOSS pre_BLDLOSS pre_ANEMDEF pre_ALCOHOL pre_DRUG pre_PSYCH pre_DEPRESS
;
%do i=1 %to %sysfunc(countw(&names,%str( )));
%let next=%scan(&names,&i,%str( ));
.... code that does something with &NEXT ....
%end;
@RobertWF1 wrote (bold italics mine):
I have a list of 28 variable names I'd like to convert to x1-x28 in order to use a macro do loop for running proc genmod.
Why does using a macro do loop require renaming all vars to x1-x28? Folks commonly do macro loops over a list of variables with very distinct names, having no common root. Depending on the structure of your macro, you may not any renaming to facilitate a loop.
I have to run a series of univariate proc genmods on my dataset, a simple solution seemed to be rename my variables x1 - x28 so I could cycle through them with a do loop over i=1 to &numvars.
Running many regressions:
https://blogs.sas.com/content/iml/2017/02/13/run-1000-regressions.html
Which brings up an interesting point ... if you talk about what your ultimate goal is, rather than the mechanics of renaming variables, you get better answers. In your case no renaming is needed, no macro loops are needed, in fact to get to your ultimate goal of running many regressions you don't even need a lot of programming, as shown in the link. So please, from now on be sure to explain your ultimate goal in your initial post.
data have; set sashelp.heart; run; proc transpose data=have(obs=0) out=vname; var _all_; run; data _null_; set vname end=last; if _n_=1 then call execute('proc datasets library=work nolist nodetails; modify have; rename '); call execute(catt(_name_,'=x',_n_)); if last then call execute(';quit;'); run;
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!
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.