BookmarkSubscribeRSS Feed
hk2013
Fluorite | Level 6

 

data one;
input ID   Date total predicted;
cards;
1              6/24/18    200    250
1             6/30/18    200    270
1              6/18/20    200   230 
2               5/24/18     160   200
2             5/27/18     160   210
2             5/22/18     160   170
3          5/20/18     300   450
3           5/14/18     300   400
3            5/4/18     300     425
;

i want to pick the first date for each ID and subtract that date and IDs Predicted  from Total 

 

4 REPLIES 4
PaigeMiller
Diamond | Level 26

Show us the desired result. 

--
Paige Miller
hk2013
Fluorite | Level 6

 

result would look like this 
ID             Date       total  Predicted   diff
1              6/18/20    200    230           -30
2             5/22/18     160   170            -10
3            5/4/18     300     425           -125
Reeza
Super User

The way your data is sorted you're looking for the last. Is your data sort important and verified or do you need to verify it?

 

proc sort data=have; 
by id date;
run;

data want;
set have;
by id;
if first.id;
dif = predicted-total;
run;

Documentation references:

http://documentation.sas.com/?docsetId=lrcon&docsetTarget=n138da4gme3zb7n1nifpfhqv7clq.htm&docsetVer...

 

They have some good pictures that illustrate how BY groups work in the documentation. 

 


@hk2013 wrote:

 

data one;
input ID   Date total predicted;
cards;
1              6/24/18    200    250
1             6/30/18    200    270
1              6/18/20    200   230 
2               5/24/18     160   200
2             5/27/18     160   210
2             5/22/18     160   170
3          5/20/18     300   450
3           5/14/18     300   400
3            5/4/18     300     425
;

i want to pick the first date for each ID and subtract that date and IDs Predicted  from Total 

 


 

jebjur
SAS Employee

This code should give you the results you want:

 

data two (drop=temp);
set one;
 by ID;
 retain temp;
   if first.id then temp=total;
   else if last.id then do;
      diff=temp-predicted;
      output;
  end;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 4 replies
  • 1240 views
  • 0 likes
  • 4 in conversation