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

Hi,

 

I would like to recode a bunch of variables at the same time. For e.g. I have the following variables:

 

1) A

2) B

3) C

4) D

 

They all take values from 1 through 9. I want to recode the values for all these variables in the same manner.

E.g. if A = 1 then A = 10. The same rule applies to all the variables (A-D). Is there a way to do this using a macro or a loop? I don't want to give if-then commands individually for each of the 4 variables and am looking for a more efficient way to do this. 

 

Thanks for your help!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

SAS contains a standard tool for processing a group of variables in the same way.  Arrays do that:

 

data want;

set have;

array vars {4} a b c d;

do _n_=1 to 4;

   if vars{_n_}=1 then vars{_n_}=10;

end;

run;

 

You'll need to type all your IF/THEN statements once.  Then the DO loop changes one variable at a time, applying all the logic between DO and END.

View solution in original post

1 REPLY 1
Astounding
PROC Star

SAS contains a standard tool for processing a group of variables in the same way.  Arrays do that:

 

data want;

set have;

array vars {4} a b c d;

do _n_=1 to 4;

   if vars{_n_}=1 then vars{_n_}=10;

end;

run;

 

You'll need to type all your IF/THEN statements once.  Then the DO loop changes one variable at a time, applying all the logic between DO and END.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 6613 views
  • 2 likes
  • 2 in conversation