Recoding several similar variables

Accepted Solution Solved
Reply
Regular Contributor
Posts: 150
Accepted Solution

Recoding several similar variables

I have about 15 categorical variables that have the same response options that I need to recode. Right now they have 6 categories and I am recoding so they only have 4. Is there a way to do this so I don't have to write recode statements for each variable? Here is what my sample recode statement would look like:

herosmf=.;

if dg6fqa=. then herosmf=.;

else if (dg6fqa=1) or (dg6fqa=2) then herosmf=2;

else if (dg6fqa=3) or (dg6fqa=4) or (dg6fqa=5) then herosmf=3;

else if (dg6fqa=6) or (dg6fqa=7) then herosmf=4;

else if (dg6fqa=8) then herosmf=1;

thanks in advance for your input.


Accepted Solutions
Solution
‎07-29-2013 03:51 PM
Super User
Posts: 17,737

Re: Recoding several similar variables

Use a format.

http://www2.sas.com/proceedings/sugi30/001-30.pdf

Then you can use multiple put statements in code or an array/loop or just apply formats depending on what your'e doing.

The paper above is a great reference on how to use formats.

data want;

set have;

herosmf=put(dg6fqa, fmt1.);

herosmf2=put(dg7fqa, fmt1.);

etc...

run;

View solution in original post


All Replies
Solution
‎07-29-2013 03:51 PM
Super User
Posts: 17,737

Re: Recoding several similar variables

Use a format.

http://www2.sas.com/proceedings/sugi30/001-30.pdf

Then you can use multiple put statements in code or an array/loop or just apply formats depending on what your'e doing.

The paper above is a great reference on how to use formats.

data want;

set have;

herosmf=put(dg6fqa, fmt1.);

herosmf2=put(dg7fqa, fmt1.);

etc...

run;

Regular Contributor
Posts: 150

Re: Recoding several similar variables

That is really helpful but I am having a little trouble with the format since I have never used them like this. Here are my 8 categories:

1 = One time per month or less
2 = 2 to 3 days per month
3 = One time per week
4 = 2 to 3 days per week
5 = 4 to 6 days per week
6 = One time per day
7 = More than once per day
8 = Never

would my format look like this? This code is not working and the examples in the paper are coding from a continuous variable so I haven't been able to see an example. Thanks again for your help

proc format;
value freqfmt 1-2='2';
              3-5='3';
              6-7='4';
              8='5';
run;

Super User
Posts: 17,737

Re: Recoding several similar variables

You don't need semicolons after each line, only the last.

Regular Contributor
Posts: 150

Re: Recoding several similar variables

Of course, like a regular format! Thank you! This is much more efficient then recoding all of the variables!

Regular Contributor
Posts: 150

Re: Recoding several similar variables

One more question, it is making my variables character, how do I keep them as numeric variable? Thanks!

Super User
Posts: 17,737

Re: Recoding several similar variables

In your format you put them as "2" on the right hand side of the equation, you can replace that as 2. 
Depending on what you need to do, you may not even need to convert it, you can just use:

format var1-var10 recode_fmt. ;

Super User
Posts: 5,069

Re: Recoding several similar variables

The PUT function always returns a character string.  You'll need a more complex expression to convert it to numeric:

herosmf = input( put(dq6fqa, freqfmt.), 1.);

Regular Contributor
Posts: 150

Re: Recoding several similar variables

Thank you! Using the input statement worked. It is good to know that the put statement always produces the a character string.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 289 views
  • 7 likes
  • 3 in conversation