Hello Community,
I remember being able to map variable value into LINE statement text in the past, but cannot figure out it now. I believe there is an elementary item that I'm missing (which is unfamiliar to me).
Currently, to map a variable value into the LINE I keep hardcoding, eg. using IF/ELSE (see below #1). Could anyone fix #2 and explain how to avoid hardcoding part?
Many thanks!
data have;
input visitnum visit $;
if visit eq 'Screen' then do score= 1 to 5; output; end;
else do score= 1 to 6; output; end;
cards;
1 Screen
2 Week_1
3 Week_2
4 Week_3
;
run;
proc format;
value score 1= 'Very severe'
2= 'Severe'
3= 'Moderate'
4= 'Mild'
5= 'Minimal'
6= 'No pain';
run;
/* 1. Current coding */
proc report data=have;
column visitnum visit score;
define visitnum/order noprint;
define visit/ noprint;
define score/'Response Category' format=score. style(column)={just=l} display;
compute before visitnum/style={just=l};
line @1 text $40.;
length text $40;
if visitnum=1 then text='Screen';
else if visitnum=2 then text='Week_1';
else if visitnum=3 then text='Week_2';
else if visitnum=4 then text='Week_3';
endcomp;
run;
/* 2. Wanted coding - needs to be fixed to get the same result as #1*/
proc report data=have;
column visitnum visit score;
define visitnum/order noprint;
define visit/ noprint;
define score/'Response Category' format=score. style(column)={just=l} display;
compute before visitnum/style={just=l};
line @1 text $40.;
length text $40;
text=visit;
endcomp;
run;
Hi, It seems to me, based on your test data that VISITNUM and VISIT are tied together and change at the same time. So if you just change which variable you're using for the COMPUTE BEFORE and make VISIT an ORDER item too, it will greatly simplify your code. I changed the style for the LINE in the COMPUTE block to HEADER, so it was more prominent.
Results:
Alternate code:
/* ALTERNATE coding */
proc report data=have;
title 'ALTERNATE coding';
column visitnum visit score;
define visitnum/order noprint;
define visit/ order noprint;
define score/'Response Category' format=score. style(column)={just=l} display;
compute before visit/style=Header{just=l};
line @1 visit $40.;
endcomp;
run;
title;
Cynthia
Hi, It seems to me, based on your test data that VISITNUM and VISIT are tied together and change at the same time. So if you just change which variable you're using for the COMPUTE BEFORE and make VISIT an ORDER item too, it will greatly simplify your code. I changed the style for the LINE in the COMPUTE block to HEADER, so it was more prominent.
Results:
Alternate code:
/* ALTERNATE coding */
proc report data=have;
title 'ALTERNATE coding';
column visitnum visit score;
define visitnum/order noprint;
define visit/ order noprint;
define score/'Response Category' format=score. style(column)={just=l} display;
compute before visit/style=Header{just=l};
line @1 visit $40.;
endcomp;
run;
title;
Cynthia
Thank you, Cynthia!
"Header" element is very helpful.
data have; input visitnum visit $; if visit eq 'Screen' then do score= 1 to 5; output; end; else do score= 1 to 6; output; end; cards; 1 Screen 2 Week_1 3 Week_2 4 Week_3 ; run; proc format; value score 1= 'Very severe' 2= 'Severe' 3= 'Moderate' 4= 'Mild' 5= 'Minimal' 6= 'No pain'; run; /* 1. Current coding */ proc report data=have nowd; column visitnum visit score; define visitnum/order noprint; define visit/order noprint; define score/'Response Category' format=score. style(column)={just=l} display; compute before visit/style={just=l}; line @1 visit $40.; endcomp; run;
Thank you, @Ksharp !
Appreciate your input!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.