turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Recoding multiple variables at the same time

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

11-23-2016 11:53 AM

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!

Accepted Solutions

Solution

11-24-2016
09:25 AM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to KarD

11-23-2016 12:46 PM

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.

All Replies

Solution

11-24-2016
09:25 AM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to KarD

11-23-2016 12:46 PM

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.