BookmarkSubscribeRSS Feed
rj997
Fluorite | Level 6

Hey all!

 

I have a table that looks like this:

 

ID  TERM  CUR GPA

ABC 1 3.5

ABC 2 3.67

ABC 3 3.43

XYZ 1 2.67

XYZ 2 2.69

XYZ 3 3.01

 

I want to calculate the Percent change for each group of Student ID and then the trend for each term.

I have been trying to calculate the percent change for each student ID as:

a = lag(CUR_GPA)

change = ((current-a)/a)

mean = mean(change)

If I follow this the 'a' value for each first student ID will be 0 because I wish to group by each ID and do these calculations on particular groups.

 

Thank you

 

5 REPLIES 5
mkeintz
PROC Star

Do you want a trend, or do your want record-by-record changes?   You code suggests the latter while you title suggest the former?

 

Please provide a example of how you want the results to look like.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
rj997
Fluorite | Level 6

Hey! Thanks for replying.

I want to calculate the trend based on the record by record change i.e. a mean of the change.

 

Thanks

mkeintz
PROC Star

And I repeat, how do your want you output to look?  Do you want a report, without saving a new dataset file?  Do you want a dataset with one observation per id, containing a new average percent change?  Something else?

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
rj997
Fluorite | Level 6

I am trying to create a new data set with one observation per id.

 

Thanks

mkeintz
PROC Star

You apparently want the average relative change within a group, not a trend as defined by a regression line slope.  If so:

 

data have;
  input ID $3. TERM  CUR_GPA;
datalines;
ABC 1 3.5
ABC 2 3.67
ABC 3 3.43
XYZ 1 2.67
XYZ 2 2.69
XYZ 3 3.01
run;
data want (keep=id mean_dif);
  set have;
  by id;
  difsum+dif(cur_gpa);
  nid+1;
  if first.id then do;
     difsum=0;
     nid=1;
  end;
  if last.id;
  mean_dif=difsum/(nid-1);
run;

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 5 replies
  • 1377 views
  • 0 likes
  • 2 in conversation