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
- /
- Computing Percentage Changes

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
- Highlight
- Email to a Friend
- Report Inappropriate Content

02-16-2017 08:56 AM

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

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

Posted in reply to rfortin

02-16-2017 10:15 AM

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**;

All Replies

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

Posted in reply to rfortin

02-16-2017 09:02 AM

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 ?

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

Posted in reply to rfortin

02-16-2017 09:20 AM

Look at proc expand for creating the new variable.

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

Posted in reply to rfortin

02-16-2017 09:29 AM - edited 02-16-2017 09:30 AM

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
```

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

Posted in reply to rfortin

02-16-2017 10:12 AM

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

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

Posted in reply to rfortin

02-16-2017 10:15 AM

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**;

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

Posted in reply to mkeintz

02-16-2017 12:17 PM

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