Help using Base SAS procedures

creating a new variable with some conditions

Reply
Super Contributor
Posts: 338

creating a new variable with some conditions

Hi colleagues,
I have a data set (10,000 obs) like this .

Data MDI;
Input ID Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10; /*ID=Household ID*/
cards;
1 1 2 2 1 2 1 2 1 1 1
2 2 2 2 1 1 1 1 1 1 1
3 1 1 1 1 1 1 1 1 2 1
;
Run;

I need to create a new variable called deprived where deprived=1 if you get two or more 2 s for variables Q1 to Q10, else deprived =2

For instance, the new variable deprived for these three households should get following values:

For first household, deprived =1 (because there are four 2 s)
For second household, deprived =1 (because there are three 2 s)
For third household, deprived =2 (because there is only one 2 )

Could you please help me know with the codes to create the new variable called deprived?

Thanks

Neil
Respected Advisor
Posts: 4,173

Re: creating a new variable with some conditions

Neil

Your sample shows only the values '1' or '2' for the q variables. If this is true then the following should work:

if sum(of qSmiley Happy>=12 then deprived=2;
else deprived=1;

HTH
Patrick

Message was edited by: Patrick
Super Contributor
Posts: 338

Re: creating a new variable with some conditions

Hi Patrick,

Your codes worked perfectly. Many thanks.

However, I couldn't mention that some households responded for Q variables like this:
Don’t know or Refused or Not stated.
The codes for above three responses are 7, 8 and 9 respectively.

In that case, how could I handle this situation?

Thanks

Neil
Respected Advisor
Posts: 3,799

Re: creating a new variable with some conditions

concatenate, compress, count, test

[pre]
deprived = ifN(lengthN(compress(cats(of qSmiley Happy,'2','K')) ge 2,1,2);
[/pre]
Ask a Question
Discussion stats
  • 3 replies
  • 109 views
  • 0 likes
  • 3 in conversation