BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ChuksManuel
Pyrite | Level 9

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;
1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
SAS Super FREQ
Hi:
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

View solution in original post

3 REPLIES 3
Cynthia_sas
SAS Super FREQ
Hi:
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
ChuksManuel
Pyrite | Level 9
The mistake was in the final else statement. Switched them and it worked. Thanks!
Tom
Super User Tom
Super User

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.);

SAS Innovate 2025: Register Now

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1778 views
  • 1 like
  • 3 in conversation