BookmarkSubscribeRSS Feed
spg
Obsidian | Level 7 spg
Obsidian | Level 7

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!

4 REPLIES 4
Reeza
Super User

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';

ballardw
Super User

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_2:) = n (of m_2:) then Status='Married';

else if sum (of m_2:)=0 then Status = "Divorced;

else status='NA';

Ksharp
Super User

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;

spg
Obsidian | Level 7 spg
Obsidian | Level 7

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!

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 773 views
  • 0 likes
  • 4 in conversation