Hi,
I wish to create a COMPUTE statement with a variable value.
For eg :
The value for jump can range from 1 to 50 depending on the dataset being handled.
I am hoping maybe I can create a macro as below. It throws an error however as it doesnt like the if statment etc
ERROR 180-322: Statement is not valid or it is used out of proper order.
What I want to be able to do is call this macro within a proc report based on what value I need to assign for jump.
If its not a macro is there another way to do it please. All I really want is to have the compute statement created by replacing the value for jump within the compute statment.
%macro Name(jump);
COMPUTE NAME&jump.;
if CR&jump.=1 AND CL&jump.=1 then call define ("COL&jump.",'style','style={font_weight=bold background=&clr1.}');
if CR&jump.=2 AND CL&jump.=1 then call define ("COL&jump.",'style','style={font_weight=bold background=&clr2.}');
if CR&jump.=3 AND CLR&jump.=1 then call define ("COL&jump.",'style','style={font_weight=bold background=&clr3.}');
if CR&jump.=4 AND CLR&jump.=1 then call define ("COL&jump.",'style','style={font_weight=bold background=&clr4.}');
endcomp;
%mend;
Let's progress one step further:
proc report data=TEST_RPT style(column)={tagattr='wraptext:no' }
style(header)={just=l fontfamily=arial fontweight=bold foreground=black background=tan fontsize=2.5}
style(column)={fontfamily=coolvetica fontsize=2};
column CLR1-CLR&mcolcnt. CLFR1-CLFR&mcolcnt. &mcols. ;
define CLFR1-CLFR&mcolcnt. /display noprint;
define CLR1-CLR&mcolcnt. /display noprint;
%macro j(num);
compute NAME&num.;
if CLFR&num.=1 AND CLR&num.=1 then
call define ("NAME&num.",'style','style={font_weight=bold background=&clr1.}');
if CLFR&num.=2 AND CLR&num.=1 then
call define ("NAME&num.",'style','style={font_weight=bold background=&clr2.}');
if CLFR&num.=3 AND CLR&num.=1 then
call define ("NAME&num.",'style','style={font_weight=bold background=&clr3.}');
if CLFR&num.=4 AND CLR&num.=1 then
call define ("NAME&num.",'style','style={font_weight=bold background=&clr4.}');
endcomp;
%mend;
%j(14)
run;
Copy/paste the code to your environment and run it.
Please run the following command
options mprint;
at the start of your program, run the program again and show us the LOG. We need to see the ENTIRE log for PROC REPORT, that's 100%, every single character of the LOG for PROC REPORT — do not pick and choose parts of the log for PROC REPORT to show us. Please preserve the formatting in the log by copying the log as text and then pasting it into the window that appears when you click on the </> icon — DO NOT SKIP THIS STEP.
Please read this post again and use the indicated button for posting the log!
The post includes a picture that shows the button.
And use the "little running man" right next to it for code.
Run this code:
proc report data=TEST_RPT style(column)={tagattr='wraptext:no' }
style(header)={just=l fontfamily=arial fontweight=bold foreground=black background=tan fontsize=2.5}
style(column)={fontfamily=coolvetica fontsize=2};
column CLR1-CLR&mcolcnt. CLFR1-CLFR&mcolcnt. &mcols. ;
define CLFR1-CLFR&mcolcnt. /display noprint;
define CLR1-CLR&mcolcnt. /display noprint;
%let num=14;
compute NAME&num.;
if CLFR&num.=1 AND CLR&num.=1 then
call define ("NAME&num.",'style','style={font_weight=bold background=&clr1.}');
if CLFR&num.=2 AND CLR&num.=1 then
call define ("NAME&num.",'style','style={font_weight=bold background=&clr2.}');
if CLFR&num.=3 AND CLR&num.=1 then
call define ("NAME&num.",'style','style={font_weight=bold background=&clr3.}');
if CLFR&num.=4 AND CLR&num.=1 then
call define ("NAME&num.",'style','style={font_weight=bold background=&clr4.}');
endcomp;
run;
and post the log as requested.
PS the buttons may not be there if you hit "quick reply" or answer directly from a mail. Open communities.sas.com in a browser (Chrome always works for me) to post here.
Let's progress one step further:
proc report data=TEST_RPT style(column)={tagattr='wraptext:no' }
style(header)={just=l fontfamily=arial fontweight=bold foreground=black background=tan fontsize=2.5}
style(column)={fontfamily=coolvetica fontsize=2};
column CLR1-CLR&mcolcnt. CLFR1-CLFR&mcolcnt. &mcols. ;
define CLFR1-CLFR&mcolcnt. /display noprint;
define CLR1-CLR&mcolcnt. /display noprint;
%macro j(num);
compute NAME&num.;
if CLFR&num.=1 AND CLR&num.=1 then
call define ("NAME&num.",'style','style={font_weight=bold background=&clr1.}');
if CLFR&num.=2 AND CLR&num.=1 then
call define ("NAME&num.",'style','style={font_weight=bold background=&clr2.}');
if CLFR&num.=3 AND CLR&num.=1 then
call define ("NAME&num.",'style','style={font_weight=bold background=&clr3.}');
if CLFR&num.=4 AND CLR&num.=1 then
call define ("NAME&num.",'style','style={font_weight=bold background=&clr4.}');
endcomp;
%mend;
%j(14)
run;
Copy/paste the code to your environment and run it.
Run the code without the macro definition, and set jump with a %LET.
Then, post the log by copy/pasting it into a window opened with this button:
You also seem to use macro variables clr1 to clr4 within single quotes.
Hi:
I always recommend getting things working without macro because it is easier to debug. You don't show ALL your code and you don't show your ERROR messages in the log. One reason it may not like your IF statement is that you may be referencing the variable incorrectly. Remember that for ANALYSIS items, you need to use a compound name: variable.statistic, as shown below:
But that is just a guess...you don't get any error messages with incorrect usage, just a note in the log for the variable that was referenced incorrectly.
Otherwise, without seeing ALL your code and your ODS statements and your LOG messages, it is hard to comment. Also, without providing any data, then people cannot run your code without making some fake data, which may or may not be structured correctly.
Cynthia
Where is an example of the code that worked properly without any macros?
@Anuz we specifically asked to see the LOG and we specifically gave instructions how to provide the LOG to us.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.