Hi SAS Users,
I have the following sample dataset with 3 variables: ID m_start and m_end where m_start and m_end correspond to months in a year (e.g. m_start =1 is January and m_end=12 is December; m_start=2 is February and m_end=5 is May).
data have;
ID m_start m_end
1 1 3
2 5 8
3 7 12
I would like to create 12 new variables: new_m1-new_m12 that counts the months from m_start to m_end. So my new dataset would resemble the below data set.
data want;
ID m_start m_end new_m1 new_m2 new_m3 new_m4 new_m5 new_m6 new_m7 new_m8 new_m9 new_m10 new_m11 new_m12;
1 1 3 1 1 1 . . . . . . . . .
2 5 12 . . . . 1 1 1 1 1 1 1 1
3 7 12 . . . . . . 1 1 1 1 1 1
Thanks!
Use the m_start and m_end as the start/end of your iteration in your do loop and create an empty array that you can fill in.
data want;
set have;
array new(12) new_m1-new_m12;
do i=m_start to m_end;
new(i)=1;
end;
run;
Thanks Reeza. That worked.
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 how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.