- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I don't understand a few things:
1) is POORHLTH a character variable or numeric. The first IF statement implies that the values are character, but the second ELSE IF implies that POORHLTH is numeric. A variable can only be one or the other, so one of these statements will not work as you expect.
2) your final else seems to be a logic problem. POORHLTH is the variable that already exists and poor_count is the variable you're creating. It seems to me that the final ELSE should be:
poor_count=poorhlth (you want the new value of poor_count to take on the current value of poorhlth because you know that it is not 77, 88 or 99.
3) having known people who are sick or in poor health for more than 30 days, it seems logically possible for people to be in poor health for 150 days, or 365 days or even 77 days or 88 days.
Without your whole program or some test data, it's hard to do more than guess at what might be wrong. Are you getting messages in the SAS Log? If so, what are they? Is the output as you expect? If not, what is wrong with the output you get? Can you post some test data? Can you post your entire program? That would be a help.
Cynthia
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I don't understand a few things:
1) is POORHLTH a character variable or numeric. The first IF statement implies that the values are character, but the second ELSE IF implies that POORHLTH is numeric. A variable can only be one or the other, so one of these statements will not work as you expect.
2) your final else seems to be a logic problem. POORHLTH is the variable that already exists and poor_count is the variable you're creating. It seems to me that the final ELSE should be:
poor_count=poorhlth (you want the new value of poor_count to take on the current value of poorhlth because you know that it is not 77, 88 or 99.
3) having known people who are sick or in poor health for more than 30 days, it seems logically possible for people to be in poor health for 150 days, or 365 days or even 77 days or 88 days.
Without your whole program or some test data, it's hard to do more than guess at what might be wrong. Are you getting messages in the SAS Log? If so, what are they? Is the output as you expect? If not, what is wrong with the output you get? Can you post some test data? Can you post your entire program? That would be a help.
Cynthia
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.);