Craeting a new variable- code issue

Accepted Solution Solved
Reply
Regular Contributor
Posts: 150
Accepted Solution

Craeting a new variable- code issue

I trying to fill in missing data in a dataset based on skip patterns and other variables but it does not seem to be doing what I want it to. I am not sure if there is a problem with my logic or the code and would appreciate some help.

Here is the code I have:

newvar_6m=.;

if oldvar=1 then newvar_6m=1;

else if oldvar=0 then newvar_6m=0;

else if dgeverc=0 then newvar_6m=0;

else if dgevere=0 then newvar_6m=0;

else if (dgeverc=.) and (dgevere=.) then newvar_6m=.;

The oldvar has 91 missing based on skip patterns because if participans answered no to previous questions then they skipped this question. However, I know that if they said no to previous questions, then the oldvar should also be no so  I created code to fill in the missing and create newvar_6m.

the variable dgeverc has 0 missing, and dgevere has 44 missing. I figured the code above would fill in the missing for newvar_6m so it had no missing since all missing should be recoded to 0, what did I do wrong? I am sure it is a dumb mistake I am not picking up on so any suggestions would be appreciated.


Accepted Solutions
Solution
‎08-28-2013 03:43 PM
Super User
Posts: 10,500

Re: Craeting a new variable- code issue

I think if you look closely that if dgeverc =1, or any other non-zero and non-missing value, and dgevere is missing then you do not address that case. So Oldvar=., dgeverc=1 and dgevere=. has newvar_6m not assigned. Are those what your a seeing?

View solution in original post


All Replies
Super User
Posts: 17,823

Re: Craeting a new variable- code issue

Run a proc freq on your data and examine the output (and post back if you still don't understand). 

I can't see anything wrong with the code, so either your if/else statements are off or the data isn't what you think.  Hard to tell without seeing any data or the log. 

proc freq data=have;

table oldvar*dgverc*dgevere/out=summary1 list;

run;

Solution
‎08-28-2013 03:43 PM
Super User
Posts: 10,500

Re: Craeting a new variable- code issue

I think if you look closely that if dgeverc =1, or any other non-zero and non-missing value, and dgevere is missing then you do not address that case. So Oldvar=., dgeverc=1 and dgevere=. has newvar_6m not assigned. Are those what your a seeing?

Regular Contributor
Posts: 150

Re: Craeting a new variable- code issue

I figured it out, you are correct ballardw, I have a condition that is not met but where I can't fill in the missing. They answered Yes to dgeverc and dgevere but did not answer oldvar so I don't know whether it is yea or no, therefore, I must keep those as missing values. Thank you or your input. I am just glad there was nothing wrong with my code and my logic is ok.

☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 164 views
  • 3 likes
  • 3 in conversation