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

Hello,

If I have the data like what follows, how do I generate one rtf file for each college automatically?

Data collegesummary;

input college $ program $ report $;

cards;

LLL Chinese Yes

LLL English No

LLL Spanish Yes

LLL Korean No

Engineering Mechanical Yes

Engineering Chemical Yes

Engineering Electrical No

;

run;

If I use ods.rtf and proc report, how can I create one rtf file for each college. One for LLL and one for engineering?

For LLL college, I have the following:

ods rtf path=&dir.

file="&dsn&sysdate..rtf" ;

proc report data=collegesummary nowd;

where college="LLL";

column program report;

define program/display;

define report/display;

run;quit;

ods rtf close;

If I have 20 colleges, I can't do it manually one by one, because I also specified other style options, some of which are specific to the rtf destination.

Thank you.

Yao

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
Data collegesummary;
input college $ program $ report $;
cards;
LLL Chinese Yes
LLL English No
LLL Spanish Yes
LLL Korean No
Engineering Mechanical Yes
Engineering Chemical Yes
Engineering Electrical No
;
run;
 
title;
 
%let dir = "C:\Users\Desktop\";
 
proc sort data=collegesummary(keep=college) out=unique nodupkey;by college ;run;
 
%macro repeat(dir=&dir,dsn=xxxxxxxx,group=xxxxxxxx);
ods rtf  file="&dir.&dsn&sysdate..rtf" ;
 
proc report data=collegesummary nowd;
where college="&group";
column program report;
define program/display;
define report/display;
run;quit;
 
ods rtf close;
 
%mend repeat;
 
data _null_;
set unique;
call execute('%repeat(dir=,dsn='||strip(college)||',group='||strip(college)||')');
run;

Xia Keshan

View solution in original post

4 REPLIES 4
Ksharp
Super User

Make a macro and repeat it by call execute .

proc sort data=collegesummary(keep=college) out=unique nodupkey;by college ;run;


%macro repeat(dir=xxxxxxx,dsn=xxxxxxxx,group=xxxxxxxx);
ods rtf path=&dir. file="&dsn&sysdate..rtf" ;
 
proc report data=collegesummary nowd;
where college="&group";
column program report;
define program/display;
define report/display;
run;quit;
 
ods rtf close;


%mend repeat;

data _null_;
 set unique;
 call execute('%repeat(dir=xxxxxxx,dsn='||strip(college)||',group='||strip(college)||')');
run;

Xia Keshan

Carrotbing
Calcite | Level 5

Hello Keshan,

Thanks so much for your response. However, I got this error "ERROR: No logical assign for filename XXXXXXX." when I executed the code below:

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

Data collegesummary;

input college $ program $ report $;

cards;

LLL Chinese Yes

LLL English No

LLL Spanish Yes

LLL Korean No

Engineering Mechanical Yes

Engineering Chemical Yes

Engineering Electrical No

;

run;

title;

%let dir = "C:\Users\Desktop\";

proc sort data=collegesummary(keep=college) out=unique nodupkey;by college ;run;

%macro repeat(dir=xxxxxxx,dsn=xxxxxxxx,group=xxxxxxxx);

ods rtf path=&dir. file="&dsn&sysdate..rtf" ;

proc report data=collegesummary nowd;

where college="&group";

column program report;

define program/display;

define report/display;

run;quit;

ods rtf close;

%mend repeat;

data _null_;

set unique;

call execute('%repeat(dir=xxxxxxx,dsn='||strip(college)||',group='||strip(college)||')');

run;

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

This looks very promising. I'd appreciate any solution to this error.

Thanks,

Yao

Ksharp
Super User
Data collegesummary;
input college $ program $ report $;
cards;
LLL Chinese Yes
LLL English No
LLL Spanish Yes
LLL Korean No
Engineering Mechanical Yes
Engineering Chemical Yes
Engineering Electrical No
;
run;
 
title;
 
%let dir = "C:\Users\Desktop\";
 
proc sort data=collegesummary(keep=college) out=unique nodupkey;by college ;run;
 
%macro repeat(dir=&dir,dsn=xxxxxxxx,group=xxxxxxxx);
ods rtf  file="&dir.&dsn&sysdate..rtf" ;
 
proc report data=collegesummary nowd;
where college="&group";
column program report;
define program/display;
define report/display;
run;quit;
 
ods rtf close;
 
%mend repeat;
 
data _null_;
set unique;
call execute('%repeat(dir=,dsn='||strip(college)||',group='||strip(college)||')');
run;

Xia Keshan

Carrotbing
Calcite | Level 5

It worked. Thank you so much. This is going to be very helpful!

Yao

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 4 replies
  • 1865 views
  • 3 likes
  • 2 in conversation