Hi,
This follows from a question I'd posted earlier. My data looks like this, each person's purchase of a particular product at a given year...and his/her marital status over the years (Married or Divorced). I need a single column that will give me the marital status according to year of purchase. So for person 1, the STATUS should be Married, person 2 Married, person 3 Divorced etc.
How can I get at this?
Person | Year of purchase | M_2009 | M_2010 | M_2011 | M_2012 | D_2009 | D_2010 | D_2011 | D_2012 | STATUS |
1 | 2009 | 1 | 1 | 0 | 0 | 0 | 0 | 1 | 1 | Married |
2 | 2012 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | Married |
3 | 2012 | 0 | 1 | Divorced | ||||||
4 | 2011 | 1 | 0 | 0 | 1 | |||||
5 | 2009 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | |
6 | 2010 | 0 | 0 | 0 | 1 | 1 | 1 | |||
7 | 2011 | 1 | 1 | 0 | 0 |
Thanks!
Try the vvaluex function.
data test;
set have;
Status=ifc(vvaluex("M_"||put(year_purchase, 4.))=1, "Married", "Divorced");
run;
Untested, but it should be working:
data want;
set have;
array marr {2009:2012} M_2009 - M_2012;
length status $ 8;
if marr{year}=1 then status='Married';
else status='Divorced';
run;
I guess if I were looking for job security, I might code this after the LENGTH statement:
status=substr('DivorcedMarried ', 1 + marr{year}*8, 8);
All the code does assume that Year is always within range, always takes on the right value, and that the Divorced/Married pairs are always the opposite of one another for any given year.
Good luck.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.