BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
rfarmenta
Obsidian | Level 7

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

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

else newvar=0;

View solution in original post

8 REPLIES 8
Reeza
Super User

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

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

else newvar=0;

rfarmenta
Obsidian | Level 7

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

rfarmenta
Obsidian | Level 7

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!

Reeza
Super User

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;

rfarmenta
Obsidian | Level 7

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.

ballardw
Super User

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.

rfarmenta
Obsidian | Level 7

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!

Reeza
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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