I'm trying to make a binary variable (0, 1) from a variable that has 3 levels, and is character. When I try this code -
data work.cn202103;
set nsf10.cn202103;
if vax_status=. then vstat=.;
if vax_status="No plan to vax" then vstat=1;
if vax_status="Plan to vax" or vax_status="Already vaxxed" then vstat=1;
run;
Or this code -
data work.cn202103;
set nsf10.cn202103;
if vax_status=. then vstat=.;
else if vax_status="No plan to vax" then vstat=1;
else vstat=1;
run;
I get a TON of error messages like this one -
NOTE: Invalid numeric data, vax_status='Already vaxxed' , at line 209 column 4.
I can't figure out what I'm doing wrong. Let me know if you need more info, and any help is much appreciated. Thanks.
The first time vax_status appears, you are testing to see if it is equal to a dot, and that means vax_status is numeric. The next times you test vax_status, you test to see if it is equal to a character string. Vax_status can't be both numeric and character. It must be one or the other. This can be deduced via looking at PROC CONTENTS, and/or looking at the actual data values for Vax_status.
The first time vax_status appears, you are testing to see if it is equal to a dot, and that means vax_status is numeric. The next times you test vax_status, you test to see if it is equal to a character string. Vax_status can't be both numeric and character. It must be one or the other. This can be deduced via looking at PROC CONTENTS, and/or looking at the actual data values for Vax_status.
Tip: from now on, use the MISSING function, which works properly on both character and numeric values. You should be able to use it without having to first figure out if the variable is character or numeric.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.