BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
rishabhns
Calcite | Level 5

Hi

I'm trying to create make a Macro Do Loop that will create individual reports like the one attached for the Proc Print procedure. But every time I run the code below, it just gets put into the log but no results are printed:

 

%macro patients;
%do i=1101 %to i=1109;
proc report data=Report3.Clinical3Sort nofs;
where PATIENT_ID=&i.;
title "Blood Pressure Med Study";
column PATIENT_ID DRUG SEX VISIT_DATE SYSTOLIC DIASTOLIC FEVER NAUSEA RASH;
define PATIENT_ID / group;
define DRUG / group;
define SEX / group;
define VISIT_DATE / display;
define SYSTOLIC / display;
define DIASTOLIC / display;
define FEVER / display;
define NAUSEA / display;
define RASH / display;
run;
%end;
%mend;

%patients;

 

Any help is appreciated

 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

You don't need a macro for this - just a BY PATIENT_ID statement.  If your dataset Report3.Clinical3Sort is already sorted by ID, you can just do this:

 

 

proc report data=Report3.Clinical3Sort nofs;
  by PATIENT_ID;
  where patient_id between 1101 and 1109;
  title "Blood Pressure Med Study";
  column PATIENT_ID DRUG SEX VISIT_DATE SYSTOLIC DIASTOLIC FEVER   NAUSEA RASH;
  define PATIENT_ID / group;
  define DRUG / group;
  define SEX / group;
  define VISIT_DATE / display;
  define SYSTOLIC / display;
  define DIASTOLIC / display;
  define FEVER / display;
  define NAUSEA / display;
  define RASH / display;
run;

 

And if the data aren't sorted by ID, just create a sorted dataset to submit to proc report:

 

proc sort data=Report3.Clinical3Sort  out=temp;
  where patient_id between 1101 and 1109;
  by patient_id;
run;

proc report data=temp  nofs;
  by patient_id;
.......
run;

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

2 REPLIES 2
mkeintz
PROC Star

You don't need a macro for this - just a BY PATIENT_ID statement.  If your dataset Report3.Clinical3Sort is already sorted by ID, you can just do this:

 

 

proc report data=Report3.Clinical3Sort nofs;
  by PATIENT_ID;
  where patient_id between 1101 and 1109;
  title "Blood Pressure Med Study";
  column PATIENT_ID DRUG SEX VISIT_DATE SYSTOLIC DIASTOLIC FEVER   NAUSEA RASH;
  define PATIENT_ID / group;
  define DRUG / group;
  define SEX / group;
  define VISIT_DATE / display;
  define SYSTOLIC / display;
  define DIASTOLIC / display;
  define FEVER / display;
  define NAUSEA / display;
  define RASH / display;
run;

 

And if the data aren't sorted by ID, just create a sorted dataset to submit to proc report:

 

proc sort data=Report3.Clinical3Sort  out=temp;
  where patient_id between 1101 and 1109;
  by patient_id;
run;

proc report data=temp  nofs;
  by patient_id;
.......
run;

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
ballardw
Super User

The log showing submitted lines but no results is a typical behavior after some of the common syntax errors when learning the macro language. Creating something with unbalanced quotes, parentheses, or improperly ended statements can place the system into an unsteady state.

 

You likely have to close the SAS session and restart.

 

I would suggest that until you get experience with the macro language that you always set OPTIONS MPRINT; before running any macro and then reading the log closely. Almost any warning is something to be investigated. Also with MPRINT on then any messages appear in better relation to the issue instead of a series of notes at the end of the macro execution.

SAS Innovate 2025: Register Now

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 638 views
  • 0 likes
  • 3 in conversation