BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Nasser_DRMCP
Lapis Lazuli | Level 10
data have;
  input client $ date1 date9. event1 $
    date2 date9. event2 $
    date3 date9. event3 $
    imp1: date9. imp2: date9. imp3: date9. ;
  format date: imp: date9. ;
datalines ;
12345 01JAN2020 CREATION 01APR2020 CREATION 01JUL2020 PROLONGATION 01MAR2020 01MAY2020 01SEP2020
;

Hello,

happy new year !

 

For each imp, I would like to calculate the duration between the imp date and the greatest event among the creation event

thanks a lot in advance

 

delay1 of the imp1 = 2 = from january to mars (event 1 is the begining)

delay2 of the imp2 = 1 = from april to may (event 2 is the begining)

delay3 of the imp3 = 5 = from april to september (event 2 is the begining, event3 is ignored because not creation)

 

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

Here is a way to get what you want:

data want;
  set have;
  array delays(*) 8 delay1-delay3;
  array dates(*) 8 date1-date3;
  array events(*) $8 event1-event3;
  array imps(*) 8 imp1-imp3;
  do _N_=1 to dim(dates);
    if events(_N_)='CREATION' then
      _created=dates(_N_);
    delays(_N_)=intck('month',_created,imps(_N_));
    end;
  drop _created;
run;

View solution in original post

4 REPLIES 4
ballardw
Super User

What the heck is a "greatest event"?

 

You can get differences between date values by either subtracting, which gives days as a result, or the INTCK function to get counts of other types of intervals.

 

Since Imp1 is a date it is very unlikely to every be 2 as in

delay1 of the imp1 = 2 = from january to mars (event 1 is the begining)

so you need to provide a bit more description of your problem and show what the actual result of the data given should be.

 

BTW, best is not to have blank lines in DATALINES block

Tom
Super User Tom
Super User

It might be easier if your data was more normalized. Perhaps something like this?

data have ;
  input client event :$20. date :date. ;
  format date date9.;
cards;
12345 CREATION       01JAN2020
12345 CREATION       01APR2020
12345 PROLONGATION   01JUL2020
12345 IMPLEMENTATION 01MAR2020
12345 IMPLEMENTATION 01MAY2020
12345 IMPLEMENTATION 01SEP2020
;

So given data in this form what output would you want?

s_lassen
Meteorite | Level 14

Here is a way to get what you want:

data want;
  set have;
  array delays(*) 8 delay1-delay3;
  array dates(*) 8 date1-date3;
  array events(*) $8 event1-event3;
  array imps(*) 8 imp1-imp3;
  do _N_=1 to dim(dates);
    if events(_N_)='CREATION' then
      _created=dates(_N_);
    delays(_N_)=intck('month',_created,imps(_N_));
    end;
  drop _created;
run;
Kurt_Bremser
Super User

The first thing you need to do is put your data into a usable structure, or avoid the transposition to wide if that happened further up in your process.

Search for "double transpose" here, or with Google.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 619 views
  • 0 likes
  • 5 in conversation