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

Hi

I have used the sum function in a data step to add the value of a previous row to a current one. I was wondering if there is something similar for subtracting a value in a previous row from a value in the current data set? I did not see anything for 'subtract' and tried adding a negative value using the sum function but that did not work either:

data s1AgeCleanCare1;

set s1AgeCleanSort1;

by cnty_name startyear agecat4 exitMonthCategory;

if first.agecat4 then CumulativeNumber=OutOfHomeCare;

OutOfHomeCare + (-CumulativeNumber);

run;

Paul

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21
6 REPLIES 6
ballardw
Super User

The DIF (var) function, similar to LAG in concept, will give the difference from the previous record and the current or up to 99 records for a variable depending on DIF, DIF2, ..., DIF99. However you don't want to use it after IF statements as you will likely get unexpected results.

art297
Opal | Level 21

Couldn't you just use sum(OutOfHomeCare-CumulativeNumber);

It is documented at: http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000245953.htm

Paul_NYS
Obsidian | Level 7

Yes, I did the sum() above and that worked for me, but I think the DIF would work as well. Thank you both.

Paul

art297
Opal | Level 21

Paul, Depends upon what you need.  I didn't even see the part of your post concerning previous record.  Since the sum function retains a value across records, it could be used to start with a value and then subtract a current record's value from it.  The dif function would just get the difference between the current and previous record.

Paul_NYS
Obsidian | Level 7

Thats all I need in this particular situation actually: current - previous.

Paul

Ksharp
Super User

and take a look at function range();

data _null_;
a=4; b=1;
x=range(a,b);
put x=;
run;

Ksharp

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!

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
  • 6 replies
  • 30956 views
  • 3 likes
  • 4 in conversation