BookmarkSubscribeRSS Feed
a_zacMD
Obsidian | Level 7

Hi SAS community,

I have a huge data file of information like this: 

Current Data
IDactivity
abc1231/22/2020
abc1234/1/2020
abc1233/15/2020
abc1237/1/2020
abc1235/10/2020
bbfr451/5/2020
bbfr454/8/2020
fff6gr2/8/2020
fff6gr6/8/2020
fff6gr3/9/2020

 

But I want to be able to flip it so I can calculate date differences between the various points of activity. Is there a proc in SAS that will allow for this transformation? 

 

Wanted Data    
IDActivity 1Activity 2Activity 3Activity 4Activity 5
abc1231/22/20204/1/20203/15/20205/10/20207/1/2020
bbfr451/5/20204/8/2020   
fff6gr2/8/20203/9/20206/8/2020  

 

Thank you in advance! 

3 REPLIES 3
PaigeMiller
Diamond | Level 26

You don't need to flip the data to compute differences between dates. This code assumes that you have actual SAS dates and not a character string that looks like a date, and not SAS date/time values.

 

data want;
    set have;
    by id;
    prev_activity=lag(activity);
    if not first.id then diff=activity-prev_activity;
    drop prev_activity;
run;
--
Paige Miller
Reeza
Super User

PROC TRANSPOSE

 

Transposing data tutorials:
Long to Wide:
https://stats.idre.ucla.edu/sas/modules/how-to-reshape-data-long-to-wide-using-proc-transpose/

https://stats.idre.ucla.edu/sas/modules/reshaping-data-long-to-wide-using-the-data-step/

But you can also do it in a long method, either using a PROC EXPAND or data step.

data want;
set have;
by ID;
retain

if First.ID then first_event = activity;
time_from_first = activity - first_event;

run;

 

Angel_Larrion
SAS Employee

You can use the following code to create the table you want.

 

 

PROC TRANSPOSE data=a out=b prefix=Activity;
	 by id;
	 var activity;	
RUN;

where b is the transposed table and a is the input table.

 

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
  • 3 replies
  • 548 views
  • 0 likes
  • 4 in conversation