Hi programmers,
I have a variable poorhlth (1-30 days). 88 in the coding represents "no days of poor health". And 77, 99 represent refused/don't know. I want to creat a new variable poor_count that will set 88 as zero, and treat 77 and 99 as missing. Below is my code. What did i do wrong here?
if POORHLTH= '88' then poor_count=0;
else if POORHLTH in (77,99) then poor_count=.;
else POORHLTH= poor_count;
If you ran that code then you should still be getting concerning notes in the SAS log, since SAS will have to convert character to numeric at some point whether POORHLTH is defined as character or numeric.
788 data test; 789 input poorhlth $2. ; 790 if POORHLTH= '88' then poor_count=0; 791 else if POORHLTH in (77,99) then poor_count=.; 792 else poor_count=POORHLTH; 793 cards; NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column). 791:11 792:19 NOTE: The data set WORK.TEST has 9 observations and 2 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds 803 ; 804 data test; 805 input poorhlth 2. ; 806 if POORHLTH= '88' then poor_count=0; 807 else if POORHLTH in (77,99) then poor_count=.; 808 else poor_count=POORHLTH; 809 cards; NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column). 806:16 NOTE: The data set WORK.TEST has 9 observations and 2 variables. NOTE: DATA statement used (Total process time): real time 0.01 seconds cpu time 0.01 seconds
If POORHLTH is numeric then the encoding could be fixed with something like this:
data test;
input poorhlth 2. ;
poor_count=poorhlth;
if poor_count=88 then poor_count=0;
else if poor_count=77 then poor_count=.R;
else if poor_count=99 then poor_count=.D;
else if poor_count not in (1:30) then poor_count=.I;
cards;
0
1
2
30
31
77
88
99
.
;
Which will use special missing .R for REFUSED , .D for DO NOT KNOW, .I for an illegal code.
Can if POORHLTH is character then just change the one line to include the INPUT() function.
poor_count=input(poorhlth,32.);
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.