BookmarkSubscribeRSS Feed
dustychair
Pyrite | Level 9

Hi all,

I am trying to print all of the tables in one rtf file. However with below macro it is printing only the last one. I don't know what I am doing wrong. Could you give me any ideas/suggestion?

 

Thanks

 

%macro score(d);
data sc_inc_sdf_&d.;
set sdf.inc_sdf_&d.;
Test_score=input(TestRawScore,32.);
run;
Proc Freq data= sc_inc_sdf_&d.;
Table Test_score /Missing out=freq_&d.;
RUN;

data freq_&d.;
set freq_&d.;
Cum_Count+Count;
Cum_Percent+Percent;
RUN;
Proc Sql;
Create table score.score_dist_&d. as
Select Test_Score, Count format=comma10. Label="Count", Percent format=comma8.2 label ="Percent", Cum_Count  format=comma10. Label="Cumulative Count",
Cum_Percent format=comma8.2 label ="Cumulative Percent"
From freq_&d.;
QUIT;

ods rtf file='C:\Score\Score Distribution Tables.rtf' style = Minimal;
title "Score Distribution &d.";
proc print data=score_dist_&d. noobs label;
RUN;

%mend score;
%score(ela_1);
%score(ela_2);
%score(ela_3);
%score(ela_4);
%score(ela_5);
%score(ela_6);
%score(ela_7);
%score(MAT_1);
%score(MAT_2);
%score(MAT_3);
%score(MAT_4);
%score(MAT_5);
%score(ALG_1);
%score(ALG_2);
%score(GEO_1);

 

 

Edit: subject changed by @Kurt_Bremser 

6 REPLIES 6
Kurt_Bremser
Super User

Move the ODS RTF statement out of the macro, and issue it before the first macro call. Add an ODS RTF CLOSE statement after the final macro call.

As your code is now, every macro call overwrites the previous results.

dustychair
Pyrite | Level 9
Thank you for your answer @Kurt_Bremser . Now it is printing but the titles are messed up. For example two different tables have the same title; the first table in the document does not have a title. Not sure how SAS is thinking.
Kurt_Bremser
Super User

You have two procedures in your macro that create  ODS output: FREQ and PRINT.

Since you define the TITLE after the FREQ and before the PRINT, the first FREQ does not have a title, and the second uses the title defined in the first macro call.

Move the TITLE statement to a proper position before the first printing procedure.

ballardw
Super User

This data step is not needed:

data freq_&d.;
set freq_&d.;
Cum_Count+Count;
Cum_Percent+Percent;
RUN;

Use the OUTCUM option in Proc freq and it will create the cumulative variables you are asking for but the names will be CUM_FREQ and CUM_PCT.

Proc Freq data= sc_inc_sdf_&d.;
Table Test_score /Missing out=freq_&d.  outcum;
RUN;

 

If the only purpose of that Proc SQL step is to assign variable labels and formats then you might consider using Proc DATASETS, as that is designed for that task. You may find in the future with large data sets that the time spent doing such routine tasks as labels or formats can take a long time as a procedure like Proc SQL or a data step must process each record.

 

dustychair
Pyrite | Level 9
Thank your for your answer @ballardw . I did try proc datasets but not sure how I can change a variable's name as "Cumulative Count". I think it needs to be one word without space. Also not sure how to put commas. Thanks.
ballardw
Super User

Save yourself a lot of headaches and do not name variables that require name literals. To use variables with a name like "cumulative count" you first have to set Options validvarname=any;  Using that option may create variables when importing data sets that are obnoxious to code with.

 

Then everytime you use the variable named with a space or other non-standard character the name must be in quotes followed by an n: 'Cumulative count'n

 

I guarantee that you will constantly forget the n, the quotes or both at some point generating errors.

Much better off to have a shorter standard variable name and use the LABEL. For one thing your labels can hold a lot more characters. Second, almost every procedure using the Label will allow you to temporarily use a different version if that is useful.

 

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
  • 6 replies
  • 539 views
  • 1 like
  • 3 in conversation