Help using Base SAS procedures

Proc Report and conditional Compute statement

Accepted Solution Solved
Reply
Contributor
Posts: 71
Accepted Solution

Proc Report and conditional Compute statement

I want to insert a comment line with different text each time the value of a group variable changes in Proc Report.

I tried to use if-then-else statements with the Compute After statement but no luck so far.

I attached a sample program which I hope shows what I want to achieve, it creates a simple output.

Any help with suggestions on how to do this would be appreciated!

Thanks.

Attachment

Accepted Solutions
Solution
‎03-13-2013 01:40 AM
Trusted Advisor
Posts: 1,131

Re: Proc Report and conditional Compute statement

Hi,

you could try the below code

title;

proc report data=one nowd;

column class var1 var2;

define class / group;

define var1 / mean;

define var2 / mean;

compute after class;

    if class='A' then

    text= "  This is where I want different text according to value of A";

    else if class='B' then

    text= "  This is where I want different text according to value of B";

    else text= "  This is where I want different text according to value of C";

    line text $;

endcomp;

run;

quit;

Thanks,

Jagadish

Thanks,
Jag

View solution in original post


All Replies
Solution
‎03-13-2013 01:40 AM
Trusted Advisor
Posts: 1,131

Re: Proc Report and conditional Compute statement

Hi,

you could try the below code

title;

proc report data=one nowd;

column class var1 var2;

define class / group;

define var1 / mean;

define var2 / mean;

compute after class;

    if class='A' then

    text= "  This is where I want different text according to value of A";

    else if class='B' then

    text= "  This is where I want different text according to value of B";

    else text= "  This is where I want different text according to value of C";

    line text $;

endcomp;

run;

quit;

Thanks,

Jagadish

Thanks,
Jag
Contributor
Posts: 71

Re: Proc Report and conditional Compute statement

Thanks a lot Jagadish, it worked fine!

Regards,

Fethon

Valued Guide
Posts: 632

Re: Proc Report and conditional Compute statement

Notice that Jagadish has conditionally specified the TEXT value.  In a compute block you cannot conditionally execute LINE statements (this is unlike the DATA step where you can conditionally execute PUT statements.

Contributor
Posts: 71

Re: Proc Report and conditional Compute statement

Yes, I found out that I couldn't use the Line statement conditionally when I got an error!

Valued Guide
Posts: 632

Re: Proc Report and conditional Compute statement

The LINE statement simply cannot be conditionally executed.  This includes not only IF-THEN processing, but things like DO loops as well.

do i=1 to 10;

     line 'write text';

end;

will be processed as if we had written:

do i = 1 to 10;

end;

line 'write text';

LINE is executed only once!

Trusted Advisor
Posts: 1,131

Re: Proc Report and conditional Compute statement

Hi,

Just tried another method to get the comments inserted into the reports using proc formats

Here is the code

proc format;

    value $class "A"=" This is where I want different text according to value of A"

                 "B"=" This is where I want different text according to value of B"

                 "C"=" This is where I want different text according to value of C";

run;

title;

proc report data=one nowd;

column class var1 var2;

define class / group;

define var1 / mean;

define var2 / mean;

compute after class;

   line @1 class $class.;

endcomp;

run;

quit;

Thanks,

Jagadish

Thanks,
Jag
Contributor
Posts: 71

Re: Proc Report and conditional Compute statement

Yes, this is a neat method as well.

Thanks,

Fethon

Respected Advisor
Posts: 3,777

Re: Proc Report and conditional Compute statement

If you your condition includes "nothing" that can be simulated with $VARYING.

37763 - Conditionally print a LINE statement in PROC REPORT

proc report list nowd data=sashelp.class;
   define _all_ / display;
  
define name / order;
  
compute after name;
      length text $20;
     
if name in: ('C' 'P') then do;
         text = name;
         l=
20;
        
end;
     
else do;
         text=
' ';
         l=
0;
        
end;
     
line text $varying20. l;
      endcomp;

AlfredM       14       69    112.5
AliceF       13     56.5       84
BarbaraF       13     65.3       98
CarolF       14     62.8    102.5
Carol
HenryM       14     63.5    102.5
JamesM       12     57.3       83
JaneF       12     59.8     84.5
JanetF       15     62.5    112.5
JeffreyM       13     62.5       84
JohnM       12       59     99.5
JoyceF       11     51.3     50.5
JudyF       14     64.3       90
LouiseF       12     56.3       77
MaryF       15     66.5      112
PhilipM       16       72      150
Philip
RobertM       12     64.8      128
RonaldM       15       67      133
ThomasM       11     57.5       85
WilliamM       15     66.5      112

Contributor
Posts: 71

Re: Proc Report and conditional Compute statement

Thanks, good tip!

🔒 This topic is solved and locked.

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

Discussion stats
  • 9 replies
  • 326 views
  • 6 likes
  • 4 in conversation