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
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.
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";
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.
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.
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.
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.
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.