BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Walternate
Obsidian | Level 7

Hi,

 

I have a dataset with ID and indicators for each month/year (=1 if person had an event in that month, otherwise =.).

 

ID    event_200501   event_200502  event_200503.......event_200912

1              1                        1                       1                              .

2               .                        .                        1                              1

 

I have created an array to identify the earliest month in which a person has an event in each year, like this:

 

data want;

set have;

array  event2005(12) event_200501--event_200512;

do i=12 to 1;

if event2005(i)=1 then earliest_event_2005=i;

end;

/*Etc for all subsequent years*/

run;

 

The array runs with no error messages and earliest_event_2005 is created, but it's never actually populated (same with all other years). What I want is the exact same data but with new columns indicating the earliest event each person had in each year.

 

ID    event_200501   event_200502  event_200503.......event_200912  earliest_event_2005....earliest_event_2009

1              1                        1                       1                              .                           1                                   .

2               .                        .                        1                              1                          3                                   12

 

Any help is much appreciated. 

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hi @Walternate,

 

The issue with your code is that your iterative DO statement is incomplete:

do i=12 to 1 by -1;

Of course, ballardw's approach is more elegant.

View solution in original post

2 REPLIES 2
ballardw
Super User

Try this:

data want;
   set have;
   array  event2005(12) event_200501--event_200512;
   earliest_event_2005= whichn(min(of event2005(*)), of event2005(*));
run;
FreelanceReinh
Jade | Level 19

Hi @Walternate,

 

The issue with your code is that your iterative DO statement is incomplete:

do i=12 to 1 by -1;

Of course, ballardw's approach is more elegant.

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
  • 722 views
  • 1 like
  • 3 in conversation