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.
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
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 a lot Jagadish, it worked fine!
Regards,
Fethon
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.
Yes, I found out that I couldn't use the Line statement conditionally when I got an error!
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!
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
Yes, this is a neat method as well.
Thanks,
Fethon
If you your condition includes "nothing" that can be simulated with $VARYING.
37763 - Conditionally print a LINE statement in PROC REPORT
Alfred | M | 14 | 69 | 112.5 |
Alice | F | 13 | 56.5 | 84 |
Barbara | F | 13 | 65.3 | 98 |
Carol | F | 14 | 62.8 | 102.5 |
Carol | ||||
Henry | M | 14 | 63.5 | 102.5 |
James | M | 12 | 57.3 | 83 |
Jane | F | 12 | 59.8 | 84.5 |
Janet | F | 15 | 62.5 | 112.5 |
Jeffrey | M | 13 | 62.5 | 84 |
John | M | 12 | 59 | 99.5 |
Joyce | F | 11 | 51.3 | 50.5 |
Judy | F | 14 | 64.3 | 90 |
Louise | F | 12 | 56.3 | 77 |
Mary | F | 15 | 66.5 | 112 |
Philip | M | 16 | 72 | 150 |
Philip | ||||
Robert | M | 12 | 64.8 | 128 |
Ronald | M | 15 | 67 | 133 |
Thomas | M | 11 | 57.5 | 85 |
William | M | 15 | 66.5 | 112 |
Thanks, good tip!
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.
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.