DATA Step, Macro, Functions and more

Set variables to special missing

Accepted Solution Solved
Reply
Regular Contributor
Posts: 150
Accepted Solution

Set variables to special missing

I work with survey data and we have a short and a long survey. The long survey contain everything the short survey has with additional questions. If a person completes the short survey we want to set all of the other variables in the dataset for them to special missing, something like .l . We also have a variable to indicate the type of survey. Is there an easy way to do this? Thank you for your help.

 

 


Accepted Solutions
Solution
‎06-14-2016 01:55 PM
Super User
Posts: 10,500

Re: Set variables to special missing

Easiest might be an array of the response variables you want to set and in a data step:

supposing that questions q23-q50 are the ones you need to set special missing then:

array NA q23-q50;

do i=1 to dim(NA);

   NA[i] = .i;

end;

drop i;

 

 

Text variable don't use special missing though.

 

View solution in original post


All Replies
Solution
‎06-14-2016 01:55 PM
Super User
Posts: 10,500

Re: Set variables to special missing

Easiest might be an array of the response variables you want to set and in a data step:

supposing that questions q23-q50 are the ones you need to set special missing then:

array NA q23-q50;

do i=1 to dim(NA);

   NA[i] = .i;

end;

drop i;

 

 

Text variable don't use special missing though.

 

Regular Contributor
Posts: 150

Re: Set variables to special missing

Hmm..That code works the only problem is I have about 300 variables that are not on the short survey but 78 on the short survey. Is there a way to reverse it so I include the variable names from the short survey?

Regular Contributor
Posts: 150

Re: Set variables to special missing

Actually, I was able to get it to work. I added in the following:

 

if short[i]=. then short[i]=.l;

 

Thank you for your help!

 

 

Super User
Posts: 10,500

Re: Set variables to special missing


rfarmenta wrote:

Actually, I was able to get it to work. I added in the following:

 

if short[i]=. then short[i]=.l;

 

Thank you for your help!

 

 


Be careful with this approach if you have skip patterns in the survey data or other missing values that are not due to the survey length. You may generate special missing when not intended.

Regular Contributor
Posts: 150

Re: Set variables to special missing

Thank you. The skip patterns is not an issues because we are going to code those as special missing separately. I did realize that my code does not work for what I want though. Are there any other ways to do this? Otherwise I will have to just include all of the variable names. Thank you!

 

Super User
Posts: 10,500

Re: Set variables to special missing

It may help to use variable lists if your variable names are amenable, such as my example. If you have blocks of questions that all the names that start with some common stem that you need to address then you can use something like:

 

array NA qabc:  qpdq: ;

to shorten the amount of code you need to write. The above would place all variables whose names that start with qabc and qpdq into the array but not those that start with qabb or qpdm for instance.

or if the variables you need appear in order you can use the -- in the list to specify variables from to such as

 

array NA qabc -- qpdq; which would include the variables in order from qabc to qpdq.

You can mix these (carefully) or use multiple lists. For instance if you have blocks of questions in your long survey such that only the first question was asked in the short then array definiton might look like

 

array NA block1Q2 -- block1Qn  block2Q2 -- block2Qn ; Where block1q2 means the second question (or the first unasked in the short form) and block1Qn means the last unasked within that section of questions.

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 284 views
  • 0 likes
  • 2 in conversation