BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mhinchy
Calcite | Level 5

I have the following dataset

 

data have;
input profit;
datalines;
52
34

60

57

70
;
run;

 

I want to write a program that will create a new dataset, only containing the difference between the first and last observation?

In this case the code would show 70 (last observation) - 52 (first observation), so the output would be 18.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Here is one way:

 

data want;
  set have end=last;
  retain first;
  if _n_ eq 1 then first=profit;
  if last then do;
    result=profit-first;
    output;
  end;
run;

Art, CEO, AnalystFinder.com

 

View solution in original post

2 REPLIES 2
art297
Opal | Level 21

Here is one way:

 

data want;
  set have end=last;
  retain first;
  if _n_ eq 1 then first=profit;
  if last then do;
    result=profit-first;
    output;
  end;
run;

Art, CEO, AnalystFinder.com

 

novinosrin
Tourmaline | Level 20

hi,


data want(drop=_profit);
set have(firstobs=min);
set have(rename=(profit=_profit)) nobs=_nobs_ point=_nobs_;
d=_profit-profit;
output;
stop;
run;

 

Regards,

Naveen Srinivasan

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2 replies
  • 2212 views
  • 2 likes
  • 3 in conversation