How to identify last record of SAS data set?

sas
data_wrangling

#1

I want to update variable amount in sales data set for last observation only.

Example data set:-

Month      Amount      Product
Jan           234             AAA
Feb          123              BBB
Mar          235              BBB
Mar          345              AAA

Above, i want to change Amount (345 to 567) for Month Mar of last record only. Do we have any method to identify which is the last record?

Regards,
Steve


#2

Hi Steve,

You can use END option with SET statement that defines a temporary variable whose value is 1 when the DATA step is processing the last observation. At all other times, the value of variable is 0.

Solution

Data Ytd;
Input Month$ Amount Product$;
datalines;
Jan           234             AAA
Feb          123              BBB
Mar          235              BBB
Mar          345              AAA
;
Run;


Data Test;
Set YTD END=Temp_Var;
if Temp_Var=1 then do;
Amount=567;
end;
Run;

Regards,
Sunil


#3

Hello Sunil sir, thats fantastic codeā€¦
my question is how to update last three observations in amount variable and also how to update few observations like 2nd, 5th, 9th, 3rd any random observations.