BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
joachimg
Obsidian | Level 7

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. 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
joachimg
Obsidian | Level 7
Ahhh of course it was something that simple! Thank you so much. I changed the line to if vax_status=" " then vstat=.; Thanks again! 🙂
PaigeMiller
Diamond | Level 26

@joachimg 

 

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.

--
Paige Miller

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 445 views
  • 0 likes
  • 2 in conversation