BookmarkSubscribeRSS Feed
sophia_SAS
Obsidian | Level 7

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!

2 REPLIES 2
Reeza
Super User

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;

sophia_SAS
Obsidian | Level 7

Thanks Reeza.  That worked.

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

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
  • 2 replies
  • 726 views
  • 0 likes
  • 2 in conversation