BookmarkSubscribeRSS Feed
omerzeybek
Obsidian | Level 7

Hi I am trying to prepare a transactional data. I have  Individuals and their transaction dates. I want to sort data primarly on Customer ID and  then tranaction date. Until there it is not a big challenge, but in addition to this i require to print a new variable that states oder of every transaction by number for every individual. The final data would look like the picture i attached bellow. 

 

 

Stack_Ques.png

 

Thank you very much 

2 REPLIES 2
PaigeMiller
Diamond | Level 26
proc sort data=original_data out=sorted_data;
    by id date;
run;
data sorted_data;
    set sorted_data;
    by id;
    if first.id then sort_tranx=0;
    sort_tranx+1;
run;

 

--
Paige Miller
dcruik
Lapis Lazuli | Level 10

The code below will do what PaigeMiller's code is doing, except this will give you the same value for Sort_tranx if the ID and Date are the same.  Hope this helps!

 

data have;
input ID Date;
format Date mmddyy10.;
informat Date mmddyy10.;
datalines;
34532 01/01/2015
34532 02/03/2015
34532 05/06/2015
34532 01/01/2015
36978 01/02/2015
21562 01/01/2015
21562 01/02/2015
21562 01/03/2015
21562 01/04/2015
21562 01/05/2015
;
run;

proc sort data=have;
	by ID Date;
run;

data want;
  set have;
	by ID Date;
Date_lag=lag(Date);
	If First.ID then Sort_tranx=1;
	Else If First.ID=0 AND Date=Date_lag then do;
		Sort_tranx=Sort_tranx; end;
	Else Sort_tranx+1;
Drop Date_lag;
run;

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