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!
if (.<dg6fqad^=8) or (.<dg6fqd^=8) then newvar=1;
else if (dg6fqad=.) and (dg6fqd=.) then newvar=.;
else newvar=0;
if (.<dg6fqad^=8) or (.<dg6fqd^=8) then newvar=1;
else if (dg6fqad=.) and (dg6fqd=.) then newvar=.;
else newvar=0;
That was dumb of me, I didn't even think that all it needed was to add else. Thank you!
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!
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;
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.
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.
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!
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.