BookmarkSubscribeRSS Feed
raveena
Obsidian | Level 7
Hi,

Currently I need to calculate the change to the report by using a current count and previous count. My concern is if the prior count is ‘0’ then we can’t divide it, in that case I would like to give a condition if prior count is zero then the change should be appear as ‘100’ in the report.
I applied condition to the code as below,

Compute change;
if _freq_p.sum='0' then change='100';
else
change=(_freq_.sum -_freq_p.sum)/_freq_p.sum;
Endcomp;

But I am not getting any result, what am looking for..

Please let me know the mistakes that I have done ..



Original code
Proc report data=rpt1_compare ls=256 headline headskip split='*' nowd;
Column _freq_ _freq_p change change5;

Define _freq_/sum "CURRENT COUNT &date" width=20format=comma10. Spacing=1;
Define _freq_p/sum "PRIOR COUNT &date_p" width=20format=comma10. Spacing=1;
Define change/computed 'CHANGE %' width=12 format=percent8.2 spacing=1;

Compute change;
Change= (_freq_.sum -_freq_p.sum)/_freq_p.sum;
Endcomp;

Define change5/display 'SIGNIFICANT CHANGE >= 5%' width=20 format=$1. Spacing=1;

run;
\
Thanks in Advance
1 REPLY 1
Cynthia_sas
Diamond | Level 26
Hi:
I cannot think of any situation where varname.sum would be a character value, as you indicate by '0' in your comparison.
[pre]
if _freq_p.sum='0' then change='100';
[/pre]

I suspect that your IF test is failing. _FREQ_P should be a numeric, analysis variable if you are getting a SUM statistic for it. Therefore, there is a difference between 0 (the number) and '0' (a text character that happens to be 0). You might as well code:
[pre]
if _freq_p.sum='Z' then change='100';
[/pre]

Also, if you expect CHANGE to be a numeric variable, then I would expect the change='100' to be causing some automatic conversion messages. If you want CHANGE to be a character variable, then you would have to alter your COMPUTE block to:
[pre]
Compute change / character length=5;
. . . more code . . .
Endcomp;
[/pre]

The DIVIDE function allows you to divide and if the denominator is 0, the DIVIDE function was invented so you would not get the divide by zero error messages.

I suspect that just a little more cleanup will have your program working.

cynthia

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 981 views
  • 0 likes
  • 2 in conversation