BookmarkSubscribeRSS Feed
raveena
Obsidian | Level 7

Hi all,

I have a dataset 'Test' with the columns type, _freq_, _freq_p and change.

Need to calculate the changes for prior count in percentage and in case if

Current count is zero the change should display as 100%.

Example:

Type  _freq_  _freq_p

Anc  20        20   

PCP    0        30

Spc    5966    5967

Output should be

Type  Currentcount Priorcount change

Anc    20              20    0.00%

PCP      0              30      100%

Spc    5966          5967    (0.02%)

I tried the below code,

proc report data=test ls=256 headline headskip split='*' nowd

      column type _freq_ _freq_p change;

      define _freq_/sum "CURRENT COUNT " width=20 format=comma10. spacing=1;

      define _freq_p/sum "PRIOR COUNT " width=20 format=comma10. spacing=1;

      define change/computed 'CHANGE %' width=20 format=percent8.2 spacing=1;

compute change;

if _freq_p.sum='' then change='1';

        else

            change=(_freq_.sum -_freq_p.sum)/_freq_p.sum;

run;

But am not getting an exact answer what am expecting ?

please help me out to fix this issue.

Thanks in Advance.

2 REPLIES 2
Cynthia_sas
SAS Super FREQ

Hi:

  Well, first your COMPUTE block is missing an ENDCOMP; statement. So that could be a problem.

  Next, since you define _FREQ_ and _FREQ_P as analysis items using the SUM statistic, they are numeric, but, yet, in your test, you are asking if _FREQ_P.SUM = '' (quote quote)...this IF condition will not be met for 2 reasons, _FREQ_P has a value on every report row (20 on the first row, 30 on the second row and 5967 on the third row of data), so this test will NEVER be true. Therefore, the ELSE condition would be the one always executed. And, _FREQ_P is the Previous Count, not the Current Count.

  The column that you have indicated as CURRENT COUNT is _FREQ_, so perhaps your IF statement should be:

IF _FREQ_.SUM = 0 then CHANGE= 1;

You should also assign a number to CHANGE, not a quoted string, or else you will get automatic conversion messages in the LOG when the '1' has to be changed to the number 1, since CHANGE is also numeric.

Also, you might be getting format size messages in the log. If it is possible for your column to have negative percents, you have to make the format big enough to accomodate that. I'd recommend a percent9.2 to be on the safe side.

 

cynthia

Haikuo
Onyx | Level 15

Try this working version of your code, and please note the changes being made:

data have;

infile cards;

input Type $  _freq_  _freq_p;

cards;

Anc  20        20

PCP    0        30

Spc    5966    5967

;

proc report data=have headline headskip split='*' nowd ;

      column type _freq_ _freq_p change;

      define change/computed 'CHANGE %' width=20 format=percent8.2 spacing=1;

compute change;

         if _c2_=0 and _c3_ > 0 then change=1;

         else if _c2_=0 and _c3_ < 0 then change=-1;

          else if _c2_=0 and _c3_ = 0 then change=0;

          else  change=(_c3_ -_c2_)/_c2_;

endcomp;

run;

Regards,

Haikuo

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2 replies
  • 1124 views
  • 0 likes
  • 3 in conversation