DATA Step, Macro, Functions and more

How do I recode a race variable keeping the same variable name?

Accepted Solution Solved
Reply
Contributor
Posts: 43
Accepted Solution

How do I recode a race variable keeping the same variable name?

I currently have a race variable categorized as Asian, Black or Africian-American, Native Hawaiian or Pacific Islander, Other or White.  I would like to change the Asian and Native Hawaiian or Pacific Islander to Other using the same race variable name.  I did not create the dataset.  I am doing secondary data analysis.  I am using SAS 9.4.

 

I tried the following code, but it did not work and did not give me an error message.  Do I have to create a new variable for race or can I use the same variable name?

if strip(race)="Native Hawaiian or Pacific Islander" OR strip(race)="Asian" then race="Other";

 

Any help that could be provided would be appreciated.

 

Thanks,

Paula


Accepted Solutions
Solution
‎10-05-2016 09:17 AM
Super User
Posts: 5,084

Re: How do I recode a race variable keeping the same variable name?

Well, you didn't tell us what the error message actually is.  So here's my guess.

 

I guess that your RACE variable is actually numeric.  The strings that you are looking at like "Asian" are actually formatted values.  So you can't apply STRIP to a numeric variable.  Here's how you would confirm this:

 

proc print data=have (obs=20);

var race;

run;

 

proc print data=have (obs=20);

var race;

format race;

run;

 

The FORMAT statement (with no format named) will remove any existing format from the variable, for the purposes of running the second PROC PRINT.  Then you can see whether the actual values are different than the displayed values.

 

View solution in original post


All Replies
Super User
Posts: 17,840

Re: How do I recode a race variable keeping the same variable name?

The concept is correct. 

You do have to make sure it's exactly matching the string, case et al.

Is it Native Hawaiian or Pacific Islander in one field or are those separate? I also find IN easier to work with.

 

 

if strip(race) in ("Native Hawaiian or Pacific Islander", "Asian")  then race="Other";
Contributor
Posts: 43

Re: How do I recode a race variable keeping the same variable name?

I just checked and it is all in one field.
Solution
‎10-05-2016 09:17 AM
Super User
Posts: 5,084

Re: How do I recode a race variable keeping the same variable name?

Well, you didn't tell us what the error message actually is.  So here's my guess.

 

I guess that your RACE variable is actually numeric.  The strings that you are looking at like "Asian" are actually formatted values.  So you can't apply STRIP to a numeric variable.  Here's how you would confirm this:

 

proc print data=have (obs=20);

var race;

run;

 

proc print data=have (obs=20);

var race;

format race;

run;

 

The FORMAT statement (with no format named) will remove any existing format from the variable, for the purposes of running the second PROC PRINT.  Then you can see whether the actual values are different than the displayed values.

 

Contributor
Posts: 43

Re: How do I recode a race variable keeping the same variable name?

Thank you for your message. I did not receive an error message which is the reason I could not figure out why it did not work.

I will try your suggestion to see if the actual variable is numeric or categorical.

 

Thanks.

Super User
Posts: 5,084

Re: How do I recode a race variable keeping the same variable name?

Sorry, I misread the part in the original post about the error message.  But the next steps wouldn't change ... run the test PROC PRINTs, and it might also help to post the log from the original program.

Super User
Posts: 10,500

Re: How do I recode a race variable keeping the same variable name?

Personally I would recommend either developing a custom format to change displayed values without changing the variable OR create an entire new variable.

 

I would bet a small pile of quarters that some time in the future you get asked to report on one or both of Asian or NHOPI. Which will be much easier if the values still exist.

Contributor
Posts: 43

Re: How do I recode a race variable keeping the same variable name?

ok. Thanks for your suggestion and response.
☑ This topic is SOLVED.

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

Discussion stats
  • 7 replies
  • 423 views
  • 2 likes
  • 4 in conversation