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.

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 991 views
  • 0 likes
  • 4 in conversation