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

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,

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

5 REPLIES 5
nehalsanghvi
Pyrite | Level 9

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

pacruz
Calcite | Level 5

Thank you!

art297
Opal | Level 21

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

Astounding
PROC Star

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.

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
  • 5 replies
  • 1478 views
  • 1 like
  • 5 in conversation