BookmarkSubscribeRSS Feed
david27
Quartz | Level 8

Hello,

 

I have this:

I want to subtract the new score for each player from the previous score of that player.

In case if the player has only one record then leave the calculated field blank.

 

Please advise.

Thank You

 

 

data have;
infile datalines dlm="|";
input id score;
datalines;
1|1
2|3
2|1
2|3
2|3
3|11
4|11
5|1
5|1
6|1
7|11
8|1
9|11
10|11
10|11
;run;

 

 

7 REPLIES 7
PaigeMiller
Diamond | Level 26
data want;
    set have;
    by id;
    prev_score=lag(score);
    if first.id then prev_score=.;
    difference=score-prev_score;
    drop prev_score;
run;
--
Paige Miller
david27
Quartz | Level 8

@PaigeMiller : 

Thank You for helping me here.

 

This solves the purpose but have a follow up question because of this:

 

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.2/lefunctionsref/n0l66p5oqex1f2n1quuopdvtcjqb.h...

 

Restriction:Note:

This function is not supported in a DATA step that runs in CAS.
The VARCHAR type is not supported for arguments in the LAG function.

 

So if the Score was provided in Character format, we cannot use lag() ?

PaigeMiller
Diamond | Level 26

Well, you probably should have mentioned in your first post that you are running in CAS.

 

I don't have any knowledge of how to get this to work in CAS. It is my understanding that LAG is not available in CAS. I know you can run code in SAS Viya that is not run under CAS, in other words, it is base SAS, but I don't know how to do that either.

 

Yes, score needs to be numeric to get this to work in any version of SAS. You can't do a subtraction on character values.

--
Paige Miller
andreas_lds
Jade | Level 19

@david27 wrote:
 

 

So if the Score was provided in Character format, we cannot use lag() ?


If score is a char variable the subtraction makes no sense at all. You will have to convert the variable to numeric before any calculation takes place. 

mkeintz
PROC Star

You can simulate the lag function with a retain statement:

 

data have;
infile datalines dlm="|";
input id score;
datalines;
1|1
2|3
2|1
2|3
2|3
3|11
4|11
5|1
5|1
6|1
7|11
8|1
9|11
10|11
10|11
;run;

data want;
  set have;
  by id;
  retain old_score .;
  if first.id then old_score=.;
  if old_score^=. then difference=score-old_score;
  output;

  old_score=score;
run;

The "trick" here is to output the current observation prior to updating the (retained) old_score variable with the current score value.

--------------------------
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

--------------------------
PeterClemmensen
Tourmaline | Level 20

I'm a bit confused here. Do you want to subtract the new score from the previous score or the previous score from the new score? The latter seems to make most sense?

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 7 replies
  • 548 views
  • 0 likes
  • 6 in conversation