Help using Base SAS procedures

Assigning Status in Survey Data

Reply
Contributor spg
Contributor
Posts: 61

Assigning Status in Survey Data

Hi,

I'm trying to assign a marital status to different individuals looking at their history over the last few years (survey data). I'm only interested in those whose status have remained same all along. So the first individual, was married the first two years and divorced later...and gets dropped out.

Individual 3 gets a status because he entered the survey in 2012...etc.

PersonM_2009M_2010M_2011M_2012D_2009D_2010D_2011D_2012STATUS
111000011NA
211110000Married
3 1 1Married
4 10 01NA
500001111Divorced
6 000 111Divorced
7 11 00 Married

How do I get at this quickly? I tried some clauses in the data steps, but cannot seem to include every possible combination. I have several thousand records.

Thanks in advance!

Super User
Posts: 19,832

Re: Assigning Status in Survey Data

set up two arrays, one for M and one for D. Then see if the max=min of the array is the same and then you know they haven't changed over the survey period.

array marriage(4) m_2009-m_2012;

array divorce(4) d_2009-d_2012;

if max(of marriage(*))=min(of marriage(*)) and max(of divorce(*))=min(of divorce(*)) and max(of marriage(*))=1 then do;

    if max(of marriage(*))=1 then status='Married';

     else status='Divorced';

end;

else status='NA';

Super User
Posts: 11,343

Re: Assigning Status in Survey Data

Unless the example data is incomplete the D variables are completely superflous for this as they are the complement of the M variable.

If sum (of m_2Smiley Happy = n (of m_2Smiley Happy then Status='Married';

else if sum (of m_2Smiley Happy=0 then Status = "Divorced;

else status='NA';

Super User
Posts: 10,041

Re: Assigning Status in Survey Data

data have;

infile cards truncover expandtabs;

input Person    M_2009    M_2010    M_2011    M_2012    D_2009    D_2010    D_2011    D_2012;

cards;

1    1    1    0    0    0    0    1    1

2    1    1    1    1    0    0    0    0

3    .    .    .    1    .    .    .    1

4    .    .    1    0    .    .    0    1

5    0    0    0    0    1    1    1    1

6    .    0    0    0    .    1    1    1

7    .    1    1    .    .    0    0    .

;

run;

data want ;

set have;

array mm{*} m:;

array dd{*} d:;

if (whichn(0,of mm{*})=0 or whichn(1,of mm{*})=0 ) and

    (whichn(0,of dd{*})=0 or whichn(1,of dd{*})=0 )

  then do;

         if whichn(1,of mm{*}) then STATUS='Married  ';

          else     if whichn(1,of dd{*}) then STATUS='Divorced';

       end;

else  STATUS='NA';

run;

Contributor spg
Contributor
Posts: 61

Re: Assigning Status in Survey Data

Thank you all. Reeza, I tried your code and tweaked it a bit to have correct entries for all three categories (it was giving me only NA and Divorced initially).

KSharp, will try your code today!

Ask a Question
Discussion stats
  • 4 replies
  • 169 views
  • 0 likes
  • 4 in conversation