I am using the online sas studio.
The code is as follows,
%macro allcylinders; proc sql noprint; select distinct Cylinders into :val1- from sashelp.cars where Cylinders ne .; quit; %put &=SQLOBS; %do i=1 %to %SQLOBS; title "&&val%i-Cylinder Cars"; proc print data=sashelp.cars noobs; var Make Model Type Origin MSRP MPG_City MPG_Highway; where Cylinders=&&val&i; run; %end; %mend allcylinders; %allcylinders
The macro compilation passes successfully. When I run the macro, it complains the following,
SQLOBS=7 WARNING: Apparent invocation of macro SQLOBS not resolved. ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: %SQLOBS ERROR: The %TO value of the %DO I loop is invalid.
%do i=1 %to %SQLOBS;
should say
%do i=1 %to &SQLOBS;
and also,
title "&&val%i-Cylinder Cars"; needs to be title "&&val&i-Cylinder Cars";
There are two trigger characters that the macro processor looks for to decide if it should intervene.
The & is used for referencing macro variables (aka SYMBOLS).
The % is used for macro statements, functions and user defined macros.
You tried to call a macro named SQLOBS instead of reference the value of a macro variable named SQLOBS.
Thank all for your help! Feel so ashamed to have made so stupid mistakes ...
sorry, don't know how to accept more than one solution ..
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.