turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- How to get a value from this year minus last year.

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

02-24-2017 10:54 AM

I have a data set that calculates trade deficit in various years. I need to calculate the trade deficit between the last year of the data set and the second to last year of the data set. Please advise,

Accepted Solutions

Solution

02-24-2017
05:26 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to pacruz

02-24-2017 05:01 PM

So that's really all that is involved? No third variable like COUNTRY? If you are only concerned with that single difference, here is a way to add it onto the final observation:

proc sort data=have;

by year;

run;

data want;

set have end=done;

deficit_diff = diff(deficit);

if done=0 then deficit_diff=.;

run;

This program calculates the deficit difference on every pair of observations, then wipes out the calculated value for all but the final pair of years.

All Replies

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to pacruz

02-24-2017 10:59 AM

Please provide a sample of the dataset preferably in data step code form that we can run. generate and then try out a solution for you.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to pacruz

02-24-2017 11:07 AM

YRDIFF() function can get years between 2 dates. If you want good answers then provide the required information, we can't see anything about what your tring to do, so post test data in the form of a datastep and required output and any other information.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

02-24-2017 11:39 AM

Thank you!

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to pacruz

02-24-2017 11:31 AM

If you just need to calculate the change (delta) of the most recent two years of your data per company, then you could use something like:

data have; set sashelp.class (rename=(sex=company height=deficit weight=year)); run; proc sort data=have; by company descending year; run; data want (keep=company delta); set have; by company; retain hold_deficit; if first.company then do; counter=1; hold_deficit=deficit; end; else do; counter+1; if counter eq 2 then do; delta=hold_deficit/deficit-1; output; end; end; run;

Art, CEO, AnalystFinder.com

Solution

02-24-2017
05:26 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to pacruz

02-24-2017 05:01 PM

So that's really all that is involved? No third variable like COUNTRY? If you are only concerned with that single difference, here is a way to add it onto the final observation:

proc sort data=have;

by year;

run;

data want;

set have end=done;

deficit_diff = diff(deficit);

if done=0 then deficit_diff=.;

run;

This program calculates the deficit difference on every pair of observations, then wipes out the calculated value for all but the final pair of years.