BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
tianerhu
Pyrite | Level 9
data A(keep=y) B(keep=x) C(keep=z);
  
  x=1;
  y=2;
  Z=3;
run;
title 'A';
proc print data = A;
run;
title 'B';
proc print data = B;
run;
title 'C';
proc print data = C;
run;

above is my code, if I want to print data set A , data set B, data set C ,  I need to input the 'proc step' three times ? Is there one method which can print all the three data sets using one proc step?

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Repeating code is the domain of the macro processor. Wrap your PRINT code into a macro:

%macro myprint(ds);
title "&ds.";
proc print data = &ds.;
run;
%mend;

and call the macro repeatedly:

%myprint(A)
%myprint(B)
%myprint(C)

and if you want to further autoamte that, use a dataset and CALL EXECUTE:

data control;
input ds $;
datalines;
A
B
C
;

data _null_;
set control;
call execute(cats('%nrstr(%myprint(',ds,'))'));
run;

The %NRSTR might be needed if you have macro code in the macro, to prevent premature resolution of such.

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

Repeating code is the domain of the macro processor. Wrap your PRINT code into a macro:

%macro myprint(ds);
title "&ds.";
proc print data = &ds.;
run;
%mend;

and call the macro repeatedly:

%myprint(A)
%myprint(B)
%myprint(C)

and if you want to further autoamte that, use a dataset and CALL EXECUTE:

data control;
input ds $;
datalines;
A
B
C
;

data _null_;
set control;
call execute(cats('%nrstr(%myprint(',ds,'))'));
run;

The %NRSTR might be needed if you have macro code in the macro, to prevent premature resolution of such.

tianerhu
Pyrite | Level 9

Thank you for your help.

AMSAS
SAS Super FREQ

There's always another way in SAS.
The code below creates a macro (multiPrint) that accepts a parameter (dslist that contains your list of datasets). The macro then determines the number of datasets, and runs PROC PRINT on each dataset.
I added %put statements to help see what happens, you will probably also want to uncomment the options statement to get further debugging information, to help understand what happens.

/* options mprint mlogic symbolgen ; */

%macro multiPrint(dslist) ;	
	%let dsCnt=%sysfunc(countw("&dslist")) ;
	%put dsCnt: &dsCnt ;
	%do i=1 %to &dsCnt ;
		%let dsname=%scan(&dslist,&i) ;
		%put &i dsname= &dsname ; 
		
		title "&dsname" ;
		proc print data=&dsname ;
		run ;
	%end ;

%mend ;

data A(keep=y) Bb(keep=x) ccC(keep=z);
  
  x=1;
  y=2;
  Z=3;
run;

%multiPrint(a bb ccc) ;
tianerhu
Pyrite | Level 9

Thanks a lot.

ballardw
Super User

@tianerhu wrote:
data A(keep=y) B(keep=x) C(keep=z);
  
  x=1;
  y=2;
  Z=3;
run;
title 'A';
proc print data = A;
run;
title 'B';
proc print data = B;
run;
title 'C';
proc print data = C;
run;

above is my code, if I want to print data set A , data set B, data set C ,  I need to input the 'proc step' three times ? Is there one method which can print all the three data sets using one proc step?


Basically no. Proc Print handles one data set at a time.

If the data is structured a bit differently you may be able to combine data sets and create separate output tables for each set using BY processing.

data one;
  input x y;
datalines;
1 11
;
data two;
  input x y;
datalines;
22 222
;
data three;
  input x y;
datalines;
333 3333
;

data combined;
   set one two three indsname=dsname;
   source=dsname;
run;

proc print data=combined;
   by notsorted source;
run;

The key bits here are the SET statement option INDSNAME in the data step creating combined. It creates a temproary variable holding the name of the data set that contributes each record. The Source=dsname makes a permanent variable in the output data set.

Then the BY in proc print creates a separate output table for each  contributing data set. The NOTSORTED option on the BY statement tells SAS the Source variable is not in sorted order so the default behavior of BY group processing causing an error if not sorted BY the values is encountered does not create an error.

 

This may not do what you want if YOUR data sets have radically different variables because the combined data set would have all the variables and could result in lots of missing values if the specific variables were not that Source data set. Which would be so ugly that the whole process would be questionable in general.

 

Another approach is to use the macro language to create a loop based on the names which basically calls proc print multiple times.

 

%macro dummy(namelist=);
   %do i = 1 %to %sysfunc(countw(&namelist.,,s ));
      %let name= %scan(&namelist.,&i,,s );
      proc print data=&name.;
      run;
   %end;
%mend;

%dummy(namelist= one two three)

Caution: typos or logic errors in macro coding  can place a SAS session into an unstable state and appear to quit working.

tianerhu
Pyrite | Level 9

Thank you, I appreciate.

SAS Innovate 2025: Call for Content

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 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1671 views
  • 0 likes
  • 4 in conversation