Help using Base SAS procedures

proc report: aggregate a computed column

Accepted Solution Solved
Reply
Regular Contributor
Regular Contributor
Posts: 156
Accepted Solution

proc report: aggregate a computed column

Dear all,

I have a proc report:

proc report data=mydata;

     column myfield1 myfield2;

     define myfield1 / group;

     define myfield2 / computed;

[..]

run;

where the values of myfield2 are either 1 oder 0.

I'd like to have now a myfield3 which ist the aggregated value of myfield2. That means, myfield3 should have the values 1 , 2, 3, 4, etc. and 1 is added as soosn as myfield2 is 1.

This is like retain in the data step. But when I use retain in a compute statement for myfield3 I get an error implicating that I can't use retain here.

Unfortunately I cannot use a data step with retain before the proc report (which is much more complicated than the simle example here) to solve the problem as myfield2 can only be computed in the proc report.

Is there any way to do this?

Best wishes

Eva


Accepted Solutions
Solution
‎01-30-2012 09:52 AM
Respected Advisor
Posts: 3,124

proc report: aggregate a computed column

Hi Eva,

Since reported items are not retained, you would need a non-report temporary variable to retain the value: The following code is just to show how to retain a variable of interest.

proc report data=sashelp.class nowd headline out=tst1;

column sex age height weight age=no col_a;

define sex / group;

define height / analysis mean;

define no / analysis n "Count"format=comma12.0;

compute col_a;

      temp1+height.mean*no;

       col_a=temp1;

endcomp;

run;

Say You want to retain col_a, but you can't do it directly by using retain or summary function, however, if you introduce a new Non-reported variable 'temp1', then you get your workaround.

HTH,

Haikuo

View solution in original post


All Replies
Solution
‎01-30-2012 09:52 AM
Respected Advisor
Posts: 3,124

proc report: aggregate a computed column

Hi Eva,

Since reported items are not retained, you would need a non-report temporary variable to retain the value: The following code is just to show how to retain a variable of interest.

proc report data=sashelp.class nowd headline out=tst1;

column sex age height weight age=no col_a;

define sex / group;

define height / analysis mean;

define no / analysis n "Count"format=comma12.0;

compute col_a;

      temp1+height.mean*no;

       col_a=temp1;

endcomp;

run;

Say You want to retain col_a, but you can't do it directly by using retain or summary function, however, if you introduce a new Non-reported variable 'temp1', then you get your workaround.

HTH,

Haikuo

Regular Contributor
Regular Contributor
Posts: 156

proc report: aggregate a computed column

Dear Haikuo,

thanx for the answer and the general rule that report items cannot be retained but temporary variables. That's a godd rule to keep in mind.

So when I run the example I see that the temporary variable temp1 was initialized with 0. I then thought about initializing it with another value and this is what works:

compute before;

     temp1 = 100;

endcomp;

Best wishes,

Eva

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 209 views
  • 0 likes
  • 2 in conversation