SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Alexxxxxxx
Pyrite | Level 9

Hi,

 

I expect to calculate the change of the 'scale' variable for each group ID from the earliest date to the latest.

 

in Table 1

IDdatescale
105/09/20118
112/06/20118
107/30/20127
108/02/20137
103/06/20157
106/04/20159
108/02/20178
110/26/20178
203/17/20056
206/14/20066
212/26/20066
203/26/20075

 

1.all observations have been sorted by ID, and date variable,

2.I would like to know how does the 'scale' variable change date by date, (i.e., change=scale[n]-scale[n-1]),

3.the first observation (i.e., the earliest date in each ID group) is 0.

and finally, I expect to get the new variable 'change' in Table 2.

Table2

IDdatescalechange
105/09/201180
112/06/201180
107/30/20127-1
108/02/201370
103/06/201570
106/04/201592
108/02/20178-1
110/26/201780
203/17/200560
206/14/200660
212/26/200660
203/26/20075-1

 

Could you please give me some suggestions about this?

Thanks in advance.

data table1;
	infile cards dsd  dlm=",";
	input
	ID $
	edate :mmddyy10.
	number 8.
	;
	format
   edate mmddyy10.
   ;
	cards;
1,05/09/2011,8
1,12/06/2011,8
1,07/30/2012,7
1,08/02/2013,7
1,03/06/2015,7
1,06/04/2015,9
1,08/02/2017,8
1,10/26/2017,8
2,03/17/2005,6
2,06/14/2006,6
2,12/26/2006,6
2,03/26/2007,5

	;;;;
run;

data want;
length number 8.;
set table1;
by ID;
if first.ID then number=0
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

data table1;
	infile cards dsd  dlm=",";
	input
	ID $
	edate :mmddyy10.
	number 8.
	;
	format
   edate mmddyy10.
   ;
	cards;
1,05/09/2011,8
1,12/06/2011,8
1,07/30/2012,7
1,08/02/2013,7
1,03/06/2015,7
1,06/04/2015,9
1,08/02/2017,8
1,10/26/2017,8
2,03/17/2005,6
2,06/14/2006,6
2,12/26/2006,6
2,03/26/2007,5
;;;;
run;

data want;
 set table1;
 by id;
 change=dif(number);
 if first.id then change=0;
run;

View solution in original post

1 REPLY 1
novinosrin
Tourmaline | Level 20

data table1;
	infile cards dsd  dlm=",";
	input
	ID $
	edate :mmddyy10.
	number 8.
	;
	format
   edate mmddyy10.
   ;
	cards;
1,05/09/2011,8
1,12/06/2011,8
1,07/30/2012,7
1,08/02/2013,7
1,03/06/2015,7
1,06/04/2015,9
1,08/02/2017,8
1,10/26/2017,8
2,03/17/2005,6
2,06/14/2006,6
2,12/26/2006,6
2,03/26/2007,5
;;;;
run;

data want;
 set table1;
 by id;
 change=dif(number);
 if first.id then change=0;
run;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 1 reply
  • 513 views
  • 0 likes
  • 2 in conversation