BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
RobertWF1
Quartz | Level 8

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?

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star

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;

View solution in original post

6 REPLIES 6
SASKiwi
PROC Star

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;
Tom
Super User Tom
Super User

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;
mkeintz
PROC Star

@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.

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
RobertWF1
Quartz | Level 8
That may be, you're asking the wrong person. 🙂

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.
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Ksharp
Super User
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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 3834 views
  • 5 likes
  • 6 in conversation