Help using Base SAS procedures

print a page by variables

Reply
Regular Contributor
Posts: 163

print a page by variables

Hello:
I am trying to print each column where each column is a page. The number of columns is unknown sometimes ( col1, col2 col3...colx).

-jl
SAS Super FREQ
Posts: 8,742

Re: print a page by variables

Hi:
I'm having a hard time visualizing what you mean by "each column is a page". So let me put an example out here that uses a "known" dataset:

SASHELP.CLASS has 5 columns: NAME, SEX, AGE, HEIGHT WEIGHT. There are 19 rows in the dataset. So, I could generate 5 PROC PRINTS, one for each column and because there are 19 rows, each PROC PRINT report would fit on one printed page (such as in a PDF or RTF file).

[pre]
ods pdf file='c:\temp\five_prt.pdf';
proc print data=sashelp.class;
var name;
run;

proc print data=sashelp.class;
var sex;
run;

proc print data=sashelp.class;
var age;
run;

proc print data=sashelp.class;
var height;
run;

proc print data=sashelp.class;
var weight;
run;
ods pdf close;
[/pre]

Consider what would happen if the PROC PRINTs were changed from SASHELP.CLASS to SASHELP.SHOES. SASHELP.SHOES has over 300 rows of data. So if all the rows for one column from the SHOES data set were printed, the output would physically occupy more than one printed page.

To me, it seems like these kinds of reports on either dataset output are probably unsatisfactory. While there are some enhancements you might make to these (above) 5 PROC PRINTS it is hard to imagine how or why this kind of report would be useful. Perhaps there's something different about your data or your desired results which would make this approach feasible.

On the other hand, if SASHELP.CLASS were sorted by AGE, and then you did a PROC PRINT using a BY statement (BY AGESmiley Wink, the report would be physically ordered by age and all the rows of data where the students were the same age would be organized together. If you used a PAGEBY (PAGEBY AGESmiley Wink statement, as well, then every age group would be on a separate page. Something like this:
[pre]
ods pdf file='c:\temp\by_age.pdf';
proc print data=sashelp.class;
by age;
pageby age;
var name sex height weight;
run;
ods pdf close;
[/pre]

cynthia
Regular Contributor
Posts: 163

Re: print a page by variables

Thank you Cynthia.
Yes I would do a pro print with a var . But say I do not know the number of variables:
proc print data = data1;
var1;
run;
..
proc print data = datax;
varx;
run;

etc.
I was looking into call execute but not sure.
thanks,
SAS Super FREQ
Posts: 8,742

Re: print a page by variables

Hi:
This code would list ALL the variables by default, in the order in which they are internally stored in the SAS data set:
[pre]
proc print data=data1;
run;

proc print data=data2;
run;
[/pre]

OR, you could use the special variable references _CHARACTER_ and _NUMERIC_ to print the variables. In this example, all the character variables would appear before all the numeric variables in the output.:
[pre]
proc print data=data1;
var _CHARACTER_ _NUMERIC_;
run;

proc print data=data2;
var _CHARACTER_ _NUMERIC_;
run;
[/pre]

I'm not sure that you need CALL EXECUTE. I still don't have a clear picture of the type of report you hope to generate. However, these papers have some useful examples:
http://www2.sas.com/proceedings/sugi29/237-29.pdf (example 8 and 9)
http://www2.sas.com/proceedings/sugi31/259-31.pdf
http://www2.sas.com/proceedings/sugi26/p017-26.pdf
http://www.lexjansen.com/pharmasug/2005/posters/po31.pdf
http://analytics.ncsu.edu/sesug/2006/TU08_06.PDF
http://www.nesug.org/proceedings/nesug08/bb/bb01.pdf

Many of the examples in these papers use Macro coding techniques. For a good introduction to the SAS Macro facility, I recommend:
http://www2.sas.com/proceedings/sugi28/056-28.pdf
http://www2.sas.com/proceedings/sugi29/243-29.pdf
http://www2.sas.com/proceedings/sugi31/038-31.pdf

And for more advanced Macro techniques, such as CALL EXECUTE, I recommend:
http://www.lexjansen.com/pharmasug/2006/technicaltechniques/tt16.pdf
http://www2.sas.com/proceedings/sugi22/CODERS/PAPER70.PDF
http://www.lexjansen.com/pharmasug/2008/po/po11.pdf
http://www2.sas.com/proceedings/sugi22/CODERS/PAPER86.PDF
http://www2.sas.com/proceedings/sugi30/027-30.pdf

cynthia
Regular Contributor
Posts: 163

Re: print a page by variables

Cynthia,
Thanks for the valuable links. They helped alot . I ended up using it since I do not know how many variables I have to print and each page contains one variable. This is what I did and maybe I needed not to use call execute , let me know how could have I done this without?
In EG, I got stuck with the max limits of more than 50 data sets though when I had 64.

/*everytime call execute is called this macro will print each data set being each page*/
%macro addit(jj);

data page&jj (keep = Label page&jj rename Label = Description);
set work.TRANSPOSEDOUT (drop= source);
run;
proc print data = page&jj;
run;
%mend addit;

/*get the number of columns in the transposed data*/
proc sql noprint;

select put(count(name), 8. -L) into:col
from dictionary.columns
where libname ='WORK' and memname ="TRANSPOSEDOUT" and name not in("Source", "Label");


quit;


/*start loop to cycle through each Page to get print it*/
data _null_;

set TRANSPOSEDOUT;

/*number =input(&columns,8.);*/

do i=1 to &col;
call execute('%addit('||i||')');

end;
run;
SAS Super FREQ
Posts: 8,742

Re: print a page by variables

Hi:
I am still confused. You seem to be using the terms "column", "variable" and "page" in a different manner than I'm used to thinking about it. At one point your post says that "each page contains one variable", but then you have (in your comments) that "this macro will print each data set being each page" and then further down "start loop to cycle through each Page to get print it". I guess I don't understand how you're using the term "page" because it almost seems like you're either equating every variable with a "page" or every data set with a "page".

Also, it looks to me like you have an error in your code. The RENAME dataset option requires parentheses and if you had a KEEP and a RENAME for the same dataset, then you would follow this model: [pre]
data mydata(keep = var1 var2 var3 rename=(currvar= newvar));
[/pre]

If I were going to write a macro program to generate a proc print for every variable in SASHELP.CLASS (my original example). I would generate it as shown in the code below. I did not use CALL EXECUTE, because for my example, I didn't need to. If all I want to do is generate a separate PROC PRINT for every column in a dataset, then I just need to find out the names of the columns in my dataset of interest. If I'm going to use a macro program so I can have a %DO loop, there's no reason to add the overhead of CALL EXECUTE, because all I want are the PRINTS. Note that I have only printed 3 obs per variable in order to minimize the amount of text posted to the forum. Program and output is shown below. The line of **************** signals a page break or new page.

Without a clear picture of either the INPUT data set or the OUTPUT you desire, I am unable to comment on your solution. If your code is generating the results you want, then that's good. If you seek to make your program more efficient or your issue is with EG limits, then you might wish to consider working with Tech Support.

cynthia

The Macro program:
[pre]
%macro prtmany(lib=, dsn= );

proc sql noprint;
select count(name) into :numcols
from dictionary.columns
where libname ="%upcase(&LIB)" and memname ="%upcase(&DSN)" ;
quit;

%let numcols = &numcols;
%put numcols is: &numcols;

proc sql noprint;
select name into :col1-:col&numcols
from dictionary.columns
where libname ="%upcase(&LIB)" and memname ="%upcase(&DSN)" ;
run;
quit;

options nodate number pageno=1 ls=64;
%do i = 1 %to &numcols;
%put -----> col&i is &&col&i;

title "One PROC PRINT for Every Column in &LIB..&DSN.";
title2 "This column is: &&col&i";


proc print data=&lib..&dsn.(obs=3) label;
var &&col&i ;
run;
%end;
%mend prtmany;
[/pre]

The macro invocation:
[pre]

ods listing;
%prtmany(lib=sashelp, dsn=class);

[/pre]

The output (***** show "page" breaks in LISTING):
[pre]
One PROC PRINT for Every Column in sashelp.class 1
This column is: Name

Obs Name

1 Alfred
2 Alice
3 Barbara
*****************************************************************
One PROC PRINT for Every Column in sashelp.class 2
This column is: Sex

Obs Sex

1 M
2 F
3 F
*****************************************************************
One PROC PRINT for Every Column in sashelp.class 3
This column is: Age

Obs Age

1 14
2 13
3 13
*****************************************************************
One PROC PRINT for Every Column in sashelp.class 4
This column is: Height

Obs Height

1 69.0
2 56.5
3 65.3
*****************************************************************
One PROC PRINT for Every Column in sashelp.class 5
This column is: Weight

Obs Weight

1 112.5
2 84.0
3 98.0
[/pre]
Ask a Question
Discussion stats
  • 5 replies
  • 242 views
  • 0 likes
  • 2 in conversation