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

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   

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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;

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

6 REPLIES 6
gamotte
Rhodochrosite | Level 12
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 ?
Reeza
Super User

Look at proc expand for creating the new variable. 

ThierryHerrie
Obsidian | Level 7

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

 

ballardw
Super User

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).

mkeintz
PROC Star

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;

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
rfortin
Calcite | Level 5

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 3112 views
  • 0 likes
  • 6 in conversation