BookmarkSubscribeRSS Feed
parmis
Fluorite | Level 6

Hello,

 

I have the following dataset. Does anyone know how I can calculate monthly percent change by ID.

Thank you in advance!

DateIDAmount
Jan-18A110
Jan-18B127
Jan-18C135
Jan-18D134
Jan-18F124
Feb-18A12
Feb-18B133
Feb-18C10
Feb-18D124
Feb-18F1 
Mar-18A142
Mar-18B167
Mar-18C16
Mar-18D1 
Mar-18F163
Mar-18L136
8 REPLIES 8
PeterClemmensen
Tourmaline | Level 20

What does your desired result look like?

parmis
Fluorite | Level 6

for example the desired outcome for February would be:

the percent should show the percent increase between Jan and February. The new amount is January Amount*(1+percent change)

DateIDpercent changenew amount
Feb-18A110%44
Feb-18B19%53
Feb-18C17%57
Feb-18D11%57
Feb-18F15%22

 

PGStats
Opal | Level 21

Those values don't seem related to your input data.

PG
mkeintz
PROC Star

How do these A1 values ;

  Jan-18 A1 10
  Feb-18 A1  2
  Mar-18 A1 42


Produce this result?
  Date   ID percent change    new amount
  Feb-18 A1      10%              44

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

--------------------------
ballardw
Super User

@parmis wrote:

for example the desired outcome for February would be:

the percent should show the percent increase between Jan and February. The new amount is January Amount*(1+percent change)

Date ID percent change new amount
Feb-18 A1 10% 44
Feb-18 B1 9% 53
Feb-18 C1 7% 57
Feb-18 D1 1% 57
Feb-18 F1 5% 22

 


And is the "new amount" supposed to be the value used in comparing with March?

ballardw
Super User

So what do you want to do when the value is missing? That will affect at least 2 calculations: the current month for the missing value and then the "percent change" for then month. Also, do you have any 0 amount values? The division for percent changes doesn't work with that either.

 

This assumes that you mean to do these calculations with ID in date order.

 

My take on this would be:

data have;
   informat date anydtdte. id $3. ;
   format date date9.;
   input Date ID Amount ;
datalines;
Jan-18 A1 10 
Jan-18 B1 27 
Jan-18 C1 35 
Jan-18 D1 34 
Jan-18 F1 24 
Feb-18 A1 2 
Feb-18 B1 33 
Feb-18 C1 0 
Feb-18 D1 24 
Feb-18 F1  . 
Mar-18 A1 42 
Mar-18 B1 67 
Mar-18 C1 6 
Mar-18 D1  . 
Mar-18 F1 63 
Mar-18 L1 36 
;
run;

proc sort data=have;
   by id date;
run;

data want;
   set have;
   by id date;
   difv = Dif(amount);
   lagv = lag(amount);
   if first.id then PctChange=0;
   else if not missing(lagv) and lagv>0 then PctChange = 100*(difv/lagv);
   /* drop difv lagv;*/
run;

Proc sort data=want;
   by date id;
run;

If your date variable is not a SAS date value then that would be something to do before sorting. The sort gets the ID values together in the correct date order. Otherwise Jan comes after Feb.

 

The last sort is to put things back in the apparent desired order. You can drop the difv and lagv variables after you are sure the result is what you want.

 

Please also note that providing data in the form of a data step allows proceed to testing code. Paste such code in a code box opened using the forum's {I} icon to preserve formatting and reduce the likelihood of odd characters being created when the forum reformats text as it does in the main window.

parmis
Fluorite | Level 6

Thanks for your help. I'm trying to calculate a percent change for amount to forecast amount in long term. If the following code doesn't work with zero variables, is there any other solution?

ballardw
Super User

@parmis wrote:

Thanks for your help. I'm trying to calculate a percent change for amount to forecast amount in long term. If the following code doesn't work with zero variables, is there any other solution?


You have to decide what the "percent of change" would be with 0 or missing values should. That could be done with additional logic but I am not going to assume any rule works for your situation as I do not know your data or what the actual "forecast" methods might be or what different approaches to assigning the percent in those situations may have on the forecast approach.

 

Does you forecast method require that there be no missing data? If so you need to find some approach that is acceptable to determine numerators and denominators for these missing or 0 cases.

 

The question would relate to why are the data missing for those that it misses. There might be a clue in the definition of what "amount" measures. Is it a count or a different type of measure such as price?

If a count, is it an actual count or number of 1,000s or similar (i.e. 1= 1000 of something). In this last case then 0 might actually refer to a count less than 500 and has been rounded down. In which case 0 may not be 0 and a "small" value could be imputed to calculate percent of change.

 

And if the previous value is an actual 0 then what percent of change makes sense for a current value of 1, 10 or 100?

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 8 replies
  • 2625 views
  • 1 like
  • 5 in conversation