DATA Step, Macro, Functions and more

Recoding missing variables --seemingly simple program not working

Reply
New Contributor
Posts: 4

Recoding missing variables --seemingly simple program not working

I have a categorical variable that has either 1) missing "." data or a category for 2) "CAH" or 3) "children's".  I need to redesignation the Missing or blank observation should really have been coded as "adult" for an adult care hospital.

 

when I ran a frequency it told me how many hospitals I had in CAHs or Children's hospitals;  

output.PNG    

 

So I tried to recode the variable with the following statement:

 

if type="." then type="adult";
else if type="CAH" then type="CAH";
else if type="Children's" then type="Children";
run;


proc freq data=weighting;
tables type;
run;

 

I received the same output as posted above.  I also tried to having the missing outside if quotes (see below)

 

if type=. then type="adult";
else if type="CAH" then type="CAH";
else if type="Children's" then type="Children";
run;

 

When I did this I got this result:

Capture.PNG

 

So all the 3 catergories became recoded as "adult" hospitals.  I don't know what I am doing wrong.  Can someone help me fix the program so that the missing data become adult hospitals and the other existing categories stay the same??

 

Thank you!!!!

K

Super User
Posts: 11,343

Re: Recoding missing variables --seemingly simple program not working

Missing is a bit different for character and numeric. the . is for numeric.

 

Try

 

if missing(type) then type='Adult';

or

if missing='' then type='Adult';  /* that is two single quotes with no space between*/

 

Trusted Advisor
Posts: 1,230

Re: Recoding missing variables --seemingly simple program not working

Hi,

 

Instead of recoding type in output, try to recode type in orginal data set weighting then run proc freq to get the desired results.

 

 

Super User
Super User
Posts: 7,076

Re: Recoding missing variables --seemingly simple program not working

One of those two data steps should have given messages about needing to convert between numeric and character data.

If TYPE is numeric then you can use TYPE=. to test for missing.

But if TYPE is character then you need to use TYPE=' ' to test for missing.

You can also use MISSING(TYPE) to test for missing.

New Contributor
Posts: 4

Re: Recoding missing variables --seemingly simple program not working

Super! Thanks so much. I didn't know that at all.
Ask a Question
Discussion stats
  • 4 replies
  • 260 views
  • 0 likes
  • 4 in conversation