Help using Base SAS procedures

now they tell me

Reply
N/A
Posts: 0

now they tell me

after creating a report with proc report I now find out that certain cells should be calculated using the sums from other rows.

I have code like
compute row_sum;

if index(varname,'Percentage') then
row_sum = mean(
%do zz=1 %to &mcount;
%if %index (&&mname&zz,&&yeardat&yy) %then %do;
%if &&mnum&zz=1 %then %do;
&&mmm&zz
%end;
%if &&mnum&zz>1 %then %do;
, &&mmm&zz
%end;
%end;
%end;
);
endcomp;

however they don't want a straight average but a proportion based on two previous row_sum values.

initially I thought can I lag the results from the previous two rows? I'd like to avoid going back into my main logic to calculate this. Can I use a lag in a compute in proc report?
SAS Super FREQ
Posts: 8,862

Re: now they tell me

Posted in reply to deleted_user
Hi:
This documentation article outlines what you can put into a COMPUTE block.
http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/a000146851.htm#a002473615
And this Tech Support note shows an example of using the LAG function:
http://support.sas.com/kb/24/543.html

although it shows the use of LAG within an IF statement, which has its own set of issues:
http://support.sas.com/documentation/cdl/en/etsug/60372/HTML/default/etsug_tsdata_sect048.htm
http://support.sas.com/kb/24/665.html
http://support.sas.com/kb/25/938.html
http://support.sas.com/kb/24/694.html

Mostly, I avoid the use of the LAG function in PROC REPORT, because PROC REPORT is building one report row at a time, it really doesn't have visibility of the previous report rows when it's writing a new report row -- unless you save out the information into temporary variables. And, while the Data step program has a PDV (program data vector) from which it can retrieve and store lagged values, PROC REPORT does not have a PDV -- it only has some buffer areas where the summarized report information is held while the report rows are being written.

It sounds like you have a fairly complicated PROC REPORT step. You may wish to work with Tech Support on this question, as they can look at your data and at your entire program and help you come up with the correct approach.

cynthia
N/A
Posts: 0

Re: now they tell me

Posted in reply to Cynthia_sas
any suggestions on how to best save a value into a temp value inside a compute block?
SAS Super FREQ
Posts: 8,862

Re: now they tell me

Posted in reply to deleted_user
Hi:
Here's an example that I had. It shows two things: there are a BUNCH of temporary variables and there's one COMPUTED variable (SILLYVAR). The difference between the two types of variables is outlined in a documentation topic entitled, "".

This is a very simple example of how certain values were saved into temporary variables in a compute block and then used -- in this case, at the end of the report.

cynthia
[pre]
ods listing close;
ods html file='c:\temp\use_temp_var.html' style=sasweb;
proc report data=sashelp.class nowd
style(lines)={just=l};
title 'Example: Using Temporary Variables';
title2 'HT12, HT14, HOLD12, HOLD14, AV12, AV14, HOLDALL and HTALL are the temp variables';
title3 'SillyVar is a COMPUTED variable';
column age n height weight sillyvar;
define age / group;
define n / 'Count';
define height / mean 'Avg Height';
define weight / mean 'Avg Weight';
define sillyvar / computed 'Silly Var';
compute sillyvar;
sillyvar = weight.mean - height.mean;
endcomp;
compute height;
if age = 12 then do; hold12 = n; ht12 = height.mean; end;
else if age = 14 then do; hold14 = n; ht14 = height.mean; end;
if _break_ = '_RBREAK_' then do; holdall = n; htall = height.mean; end;
endcomp;
rbreak after / summarize;
compute after;
av12 = hold12 / holdall;
av14 = hold14 / holdall;
line 'Average Height for 12 year olds: ' ht12 4.1 ;
line 'This group is ' av12 percent8.2 'of all ages which represents' hold12 2.0 ' divided by ' holdall 2.0;
line ' ';
line 'Average Height for 14 year olds: ' ht14 4.1 ;
line 'This group is ' av14 percent8.2 'of all ages which represents' hold14 2.0 ' divided by ' holdall 2.0;
line ' ';
line 'The overall Average Height is: ' htall 4.1;
endcomp;
run;
ods html close;
[/pre]
Ask a Question
Discussion stats
  • 3 replies
  • 232 views
  • 0 likes
  • 2 in conversation