Good Afternoon All,
I am running the below Macro code and getting some errors.
data _null_;
set hospcnt;
call symput('maxcnt',maxnum);
run;
%put &maxcnt;
/* loop to go through all hospitals */
%macro hosrep();
%do j= 1 %to &maxcnt;
data _null_;
set hospcnt;
if cnt=&j;
call symput('i',trim(left(prvorg)));
run;
data report1;
set &save..procrpt;
if prvorg="&i";
run;
%end;
%mend hosrep;
Error:
MPRINT(HOSP_LOOP): data _null_;
MPRINT(HOSP_LOOP): set hospcnt;
MPRINT(HOSP_LOOP): call symput('maxcnt',maxnum);
MPRINT(HOSP_LOOP): run;
NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
3:179
NOTE: There were 91 observations read from the data set WORK.HOSPCNT.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
user cpu time 0.00 seconds
system cpu time 0.00 seconds
memory 189.17k
OS Memory 26480.00k
Timestamp 09/24/2015 12:37:28 PM
SYMBOLGEN: Macro variable MAXCNT resolves to 91
91
SYMBOLGEN: Macro variable MAXCNT resolves to 91
SYMBOLGEN: Macro variable MAXCNT resolves to 91
NOTE: Line generated by the invoked macro "HOSREP".
1 data _null_; set hospcnt; if cnt=&j; call symput('i',trim(left(prvorg))); run; data report1;
-
22
1 ! set &save..procrpt; if prvorg="&i"; run; data report2; set &save..percents; if
1 ! prvorg="&i";
MPRINT(HOSREP): data _null_;
MPRINT(HOSREP): set hospcnt;
WARNING: Apparent symbolic reference J not resolved.
MPRINT(HOSREP): if cnt=&j;
MPRINT(HOSREP): call symput('i',trim(left(prvorg)));
MPRINT(HOSREP): run;
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant,
a datetime constant, a missing value, bitstring, INPUT, PUT.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: DATA statement used (Total process time):
real time 0.13 seconds
user cpu time 0.00 seconds
system cpu time 0.00 seconds
memory 125.64k
OS Memory 26480.00k
Timestamp 09/24/2015 12:37:28 PM
MPRINT(HOSREP): data report1;
SYMBOLGEN: Macro variable SAVE resolves to work
MPRINT(HOSREP): set work.procrpt;
WARNING: Apparent symbolic reference I not resolved.
MPRINT(HOSREP): if prvorg="&i";
MPRINT(HOSREP): run;
Looks like it would be relatively easy to run the code without the macro, replace &j with 1 and see what you get. That troubleshooting step will make everything a lot easier.
Hi,
I replaced all the macro calls with put statements and the macro variables are resolving. I had to hard code a couple of values and I changed the beginning part since it seemed unnecessarily complicated. Hope this helps a bit.
data grouch;
prvorg = 'org1'; output;
prvorg = 'org2'; output;
prvorg = 'org2'; output;
run;
%macro hosp_loop();
/* Hospital counts */
/*
proc sort data=grouch out=porgs(keep=prvorg) nodupkey;
by prvorg;
run;
data porgcnt;
set porgs;
IF PRVORG = " " THEN DELETE;
cnt+1;
run;
proc sql;
create table hospcnt as
select a.*, b.cnt, max(cnt) as maxnum
from porgs a left join porgcnt b
on a.prvorg=b.prvorg;
quit;
data _null_;
set hospcnt;
call symput('maxcnt',maxnum);
run;
*/
proc freq data = grouch noprint;
table prvorg / out = hospcnt (rename = (count = cnt) drop = percent);
run;
proc sql;
select max(cnt) into :maxcnt
from hospcnt;
quit;
%put &maxcnt;
/* Pull data from GROUCHBYEFFPER table */
%put ANA055_datapull;
/* Report data for all hospitals for both Report 3 and Report 4 (creates detail_hosp_rptX & detail_hf_rptX) */
%put ANA055_datapull3;
%put ANA055_datapull4;
%global prvorg effdt;
%let effdt = effdt; /* made up value */
%let drv7 = drv7; /* made up value */
/* loop to go through all hospitals */
%macro hosrep();
%do j= 1 %to &maxcnt;
data _null_;
set hospcnt;
if cnt=&j;
*call symput('prvorg',trim(left(prvorg)));
call symputx('prvorg',prvorg);
run;
%put j=&j., prvorg=&prvorg;
/* Report data for selected hosital (creates tables report_output_rptX) */
%put ANA055_reportdata_3(&prvorg);
%put ANA055_reportdata_4(&prvorg);
/* Create Report 3 */
%put pdf_head(l,y,8,&drv7.\ANA05501\current,ANA05501_30_&prvorg._A);
%put ANA055_report_3;
%put pdf_tail(&effdt);
/* Create Report 4 */
%put pdf_head(l,y,8,&drv7.\ANA05501\current,ANA05501_30_&prvorg._B);
%put ANA055_report_4;
%put pdf_tail(&effdt);
%end;
%mend hosrep;
%if &maxcnt=0 %then %do;
%goto skip1;
%end;
%else %hosrep;
%skip1:;
%mend hosp_loop;
%hosp_loop;
Hi,
I couldn't run all your code because I don't know what's in &save..procrpt but this code works. It may be just the difference of using call syputx rather than symput. Now that symputx exists there doesn't seem to be any reason to use symput.
https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002295697.htm
data hospcnt;
maxnum = 2;
cnt = 1; prvorg = 'org1'; output;
cnt = 2; prvorg = 'org2'; output;
run;
data _null_;
set hospcnt;
call symputx('maxcnt',maxnum);
run;
%put &maxcnt;
%macro hosrep();
%do j = 1 %to &maxcnt;
data _null_;
set hospcnt;
if cnt=&j;
call symput('i',prvorg);
run;
%put i = >&i<;
%end;
%mend hosrep;
%hosrep;
Hi ,
Here attached the entire macro loop for your reference.
%macro hosp_loop();
/* Hospital counts */
proc sort data=grouch out=porgs(keep=prvorg) nodupkey;
by prvorg;
run;
data porgcnt;
set porgs;
IF PRVORG = " " THEN DELETE;
cnt+1;
run;
proc sql;
create table hospcnt as
select a.*, b.cnt, max(cnt) as maxnum
from porgs a left join porgcnt b
on a.prvorg=b.prvorg;
quit;
data _null_;
set hospcnt;
call symput('maxcnt',maxnum);
run;
%put &maxcnt;
/* Pull data from GROUCHBYEFFPER table */
%ANA055_datapull;
/* Report data for all hospitals for both Report 3 and Report 4 (creates detail_hosp_rptX & detail_hf_rptX) */
%ANA055_datapull3;
%ANA055_datapull4;
/* loop to go through all hospitals */
%global prvorg;
%macro hosrep();
%do j= 1 %to &maxcnt;
data _null_;
set hospcnt;
if cnt=&j;
call symput('prvorg',trim(left(prvorg)));
run;
/* Report data for selected hosital (creates tables report_output_rptX) */
%ANA055_reportdata_3(&prvorg);
%ANA055_reportdata_4(&prvorg);
/* Create Report 3 */
%pdf_head(l,y,8,&drv7.\ANA05501\current,ANA05501_30_&prvorg._A);
%ANA055_report_3;
%pdf_tail(&effdt);
/* Create Report 4 */
%pdf_head(l,y,8,&drv7.\ANA05501\current,ANA05501_30_&prvorg._B);
%ANA055_report_4;
%pdf_tail(&effdt);
%end;
%mend hosrep;
%if &maxcnt=0 %then %do;
%goto skip1;
%end;
%else %hosrep;
%skip1:
%mend hosp_loop;
I agree with Reeza, you should not imbed macro definitions within each other. Another tool that may help is create a file that contains the code generated by your macro code. Then you can run the code as it would run after generated by the macro facility. All you have to do is run the following two lines before you run the macro:
options mprint mfile;
filename mprint 'path';
where path is a fully qualified path to a file which will contain the code the macro will generate.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: