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 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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