DATA Step, Macro, Functions and more

Computing Percentage Changes

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Computing Percentage Changes

I have an economic data series of positive and negative numbers and i am having difficulty computing the percentage change.  I  compute percentage change as:  [(current - prior)/prior]*100    (I am using the lag function for prior).   This works fine if the numbers are positive but errors occur in some cases with 2 negative numbers or a positive and a negative number.   For example:

 

prior  4

current 2     -50%  OK

 

prior 2

current 4     100%  OK

 

prior -4

current -2    - 50% error

 

prior -2

current -4     100% error

 

prior 4

current -2    -150%  OK

 

prior -4

current 2    -150%  error

 

prior 2

current -4   -300%  OK

 

prior -2

current 4   -300%  error   

 

 

 

 


Accepted Solutions
Solution
‎02-16-2017 11:45 AM
Valued Guide
Posts: 797

Re: Computing Percentage Changes

I think you are concerned about the sign of the pct. From -4 to -2 you get -50%, but I think you want +50%.  I.e. you want the direction of the change to determine sign of pct.

 

data series;

  input current @@;

   pct= 100*sign(dif(current))*abs(dif(current)/lag(current));

datalines;

4    2    4    -2    -4    -2    2    -4    4

run;

 

View solution in original post


All Replies
Regular Contributor
Posts: 194

Re: Computing Percentage Changes

Can you please provide an example dataset (in the form of a data step with datalines) and the program you used with a copy of the error message ?
Super User
Posts: 17,837

Re: Computing Percentage Changes

Look at proc expand for creating the new variable. 

Occasional Contributor
Posts: 5

Re: Computing Percentage Changes

[ Edited ]

Could you post your code? I've tried this example and it works fine:

23         data _null_ ;
24           prior = -2 ;
25           current = -4 ;
26           dif = ((current - prior)/prior)*100 ;
27           put dif= ;
28         run ;

dif=100
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

 

Super User
Posts: 10,500

Re: Computing Percentage Changes

You will have to describe WHY you consider some of the results "errors".

whith prior=-4 and current = -2 then your formula resolves to:

-2 - (-4) = 2

2/ -4 = -0.5

-0.5*100 = -50

 

So the -50% is correct from your formula. So either your formula is incorrect or your belief that the value is incorrect is wrong or you have not completely stated the problem.

 

As an aside, there is a companion function to LAG called DIF that is the equivalent of Var -  lag(var). So you can use Dif(var).

Solution
‎02-16-2017 11:45 AM
Valued Guide
Posts: 797

Re: Computing Percentage Changes

I think you are concerned about the sign of the pct. From -4 to -2 you get -50%, but I think you want +50%.  I.e. you want the direction of the change to determine sign of pct.

 

data series;

  input current @@;

   pct= 100*sign(dif(current))*abs(dif(current)/lag(current));

datalines;

4    2    4    -2    -4    -2    2    -4    4

run;

 

Occasional Contributor
Posts: 6

Re: Computing Percentage Changes

Yes, this is exactly what I wanted to do.  ( I.e. you want the direction of the change to determine sign of pct.)

Thanks for your help.   rfortin

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 146 views
  • 0 likes
  • 6 in conversation