Missing Data -Creating new variables

Accepted Solution Solved
Reply
Regular Contributor
Posts: 150
Accepted Solution

Missing Data -Creating new variables

I am recoding variables and am having trouble with missing data. I was to make sure if data is missing, it stays missing in the new variables so I have coded like this:

if (.<dg6fqad^=8) or (.<dg6fqd^=8) then newvar=1;

if (dg6fqad=.) and (dg6fqd=.) then newvar=.;

else newvar=0;

if (.<dg6fqe^=8) then newvar1=1;

if dg6fqe=. then newvar1=.;

else newvar1=0;

This codes makes missing data =0, how do I correct this?

Thanks!


Accepted Solutions
Solution
‎05-23-2013 05:39 PM
Super User
Posts: 17,750

Re: Missing Data -Creating new variables

if (.<dg6fqad^=8) or (.<dg6fqd^=8) then newvar=1;

else if (dg6fqad=.) and (dg6fqd=.) then newvar=.;

else newvar=0;

View solution in original post


All Replies
Solution
‎05-23-2013 05:39 PM
Super User
Posts: 17,750

Re: Missing Data -Creating new variables

if (.<dg6fqad^=8) or (.<dg6fqd^=8) then newvar=1;

else if (dg6fqad=.) and (dg6fqd=.) then newvar=.;

else newvar=0;

Regular Contributor
Posts: 150

Re: Missing Data -Creating new variables

That was dumb of me, I didn't even think that all it needed was to add else. Thank you!

Regular Contributor
Posts: 150

Re: Missing Data -Creating new variables

Actually, for this, the missing variables are still getting coded wrong, is there something else I need to do? I am sure it is simple but I keep getting it wrong.

if .<dg6fqe^=8 then newvar=1;

else if dg6fqe=. then newvar=.;

else newvar=0 ;

Thanks!

Super User
Posts: 17,750

Re: Missing Data -Creating new variables

Check if its missing first and then if its not equal to 8 second.

That seems to be what your code is trying to do.

It's a bit weird to see a less than sign and a not equal to in the same line so I'm not sure what that would be evaluating to.

if dg6fqe=. then newvar=.

else if dg6fqe ne 8 then newvar=1;

else newvar=0;

Regular Contributor
Posts: 150

Re: Missing Data -Creating new variables

hm, ok that makes sense and I see why that coding is better than mine but it is still not working. I have 79 missing values and should have 62 in my 0 category but it is giving me a 0=141 and no missing. I can't figure out what is going on. Thanks for your input.

Super User
Posts: 10,466

Re: Missing Data -Creating new variables

Sometimes instead of trying to get all of several multiple variable logics to work and exclude at each step I go through the main logic. Then add:

if dg6fqe=. then newvar=. ;

After the main assignments. It may not be elegant but it easy to understand and maintain.

Regular Contributor
Posts: 150

Re: Missing Data -Creating new variables

I just figured it out, for some reason I had two instances of the code in the data step. The second coding was incorrect and lumping the missing in with the 0 category and was overriding the first, correct coding. Dumb oversight on my part, thank you for your help and patience! I appreciate it!

Super User
Posts: 17,750

Re: Missing Data -Creating new variables

run a proc freq on the new/old var to see where isn't coding correctly.

proc freq data=have;

table dg6fqe*newvar/missing;

run;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 249 views
  • 6 likes
  • 3 in conversation