The SAS Output Delivery System and reporting techniques

ODS PDF Report - Output 10 Lines to one PDF in a non-table format

Accepted Solution Solved
Reply
Contributor
Posts: 23
Accepted Solution

ODS PDF Report - Output 10 Lines to one PDF in a non-table format

Greetings,

 

1. I have a 10X5 dataset. For example:

 

Var1Var2Var3Var4Var5
1DavidUS51
2BradCanada872
3KnightCanada54
4BillCanada65
5DonaldUS647
6HillaryUS498

 

2. I would like to iterate through each row, and output Var1, Var3 and Var5 to pdf including text in the middle. The pdf will look like that:

 

Person number <var1> Lives in <var3> and has <var5>  childerns.

Person number 1 Lives in US and has childerns.

Person number 2 Lives in Canada and has 2 childerns.

 

3. What is the best way to do this? Whenever I use ODS PDF it doesn't iterate inside the datastep, and I get only the last row. This is what i'm using:

 

data _null_;
set WORK.Temp;

J = 1;
do while(J lt 5);
ods pdf text="&var1";

J+ +1;

output;
end;

run;

 

4. Thanks.

 

D

 

 


Accepted Solutions
Solution
‎08-22-2016 04:42 AM
SAS Super FREQ
Posts: 706

Re: ODS PDF Report - Output 10 Lines to one PDF in a non-table format

[ Edited ]

Hi

 

As @KurtBremser already mentioned the ODS PDF is a global statement, and can not be executed within a DATA Step loop.

 

Since SAS9.3 there is a function DOSUBL, that allows you to execute global statements or complete steps while the DATA Step is running.

 

So find below two DATA Steps, showing two approaches, the second one is most similar to what you want to do.

 

ods pdf file="c:\temp\sample.pdf";

data _null_;
  set sashelp.class;
  length printLine $ 254;
  printLine = catx(" ", "the person named:", name, "is", age, "old");

  file print ods=(
    GENERIC=ON 
    variables=(printLine)
  );
  put printLine ;
run;


data _null_;
  set sashelp.class;
  length printLine $ 254;
  printLine = catx(" ", "the person named:", name, "is", age, "old");

  rc = dosubl(cats("ods pdf text=", quote(printLine), ";") );
run;

ods pdf close;

 

View solution in original post


All Replies
Super User
Posts: 7,444

Re: ODS PDF Report - Output 10 Lines to one PDF in a non-table format

ODS statements are not data step statements; they are executed immediately when encountered.

In your case, the ods statement is encountered when the program text is collected, using the macro variable var1. It is immediately executed, then the rest of the data step statements are collected, then the data step

data _null_;
set WORK.Temp;
J = 1;
do while(J lt 5);
  J+ +1;
  output;
end;
run;

is compiled and executed, which does not create output or a dataset at all. Note the absence of the ods statement, which has already beeen dealt with.

If you want your text in the pdf, I suggest to create a simple dataset containing one variable with the text string, and use proc print to print that to the ODS destination.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎08-22-2016 04:42 AM
SAS Super FREQ
Posts: 706

Re: ODS PDF Report - Output 10 Lines to one PDF in a non-table format

[ Edited ]

Hi

 

As @KurtBremser already mentioned the ODS PDF is a global statement, and can not be executed within a DATA Step loop.

 

Since SAS9.3 there is a function DOSUBL, that allows you to execute global statements or complete steps while the DATA Step is running.

 

So find below two DATA Steps, showing two approaches, the second one is most similar to what you want to do.

 

ods pdf file="c:\temp\sample.pdf";

data _null_;
  set sashelp.class;
  length printLine $ 254;
  printLine = catx(" ", "the person named:", name, "is", age, "old");

  file print ods=(
    GENERIC=ON 
    variables=(printLine)
  );
  put printLine ;
run;


data _null_;
  set sashelp.class;
  length printLine $ 254;
  printLine = catx(" ", "the person named:", name, "is", age, "old");

  rc = dosubl(cats("ods pdf text=", quote(printLine), ";") );
run;

ods pdf close;

 

Contributor
Posts: 23

Re: ODS PDF Report - Output 10 Lines to one PDF in a non-table format

Greetings,

 

Thanks for your prompt response! Smiley Happy This solution is perfectly fit to the question I had.

 

One more thing -> I need to format one of the vars as dollar15.2. Despite the fact that the var is already formatted in the dataset as dollar15.2, the ouput is regular int. 

 

What would be the best method to format the output number as dollar15.2?

 

TNX!

 

D

Super User
Posts: 7,444

Re: ODS PDF Report - Output 10 Lines to one PDF in a non-table format


DanielDor wrote:

Greetings,

 

Thanks for your prompt response! Smiley Happy This solution is perfectly fit to the question I had.

 

One more thing -> I need to format one of the vars as dollar15.2. Despite the fact that the var is already formatted in the dataset as dollar15.2, the ouput is regular int. 

 

What would be the best method to format the output number as dollar15.2?

 

TNX!

 

D


Use the dollar15.2 format in the respective put() function.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
SAS Super FREQ
Posts: 706

Re: ODS PDF Report - Output 10 Lines to one PDF in a non-table format

Hi

 

If the format is already assignd to the variable, you can also use the VVALUE() function.

 

Which version of the DATA Step did you choose for your program?

 

Bruno

Contributor
Posts: 23

Re: ODS PDF Report - Output 10 Lines to one PDF in a non-table format

Hi,

 

I've used the 2nd data step version.. Thanks againg! This is the code i finally used:

 

ods pdf file="C:\temp - &fdate .pdf" ;
title "^S={font_size=14pt} Report - &fdate ^S={}";
ods escapechar='^';
ods pdf text = "^{style[just=right preimage='C:\Users\Public\Pictures\Sample Pictures\tempPIC.png']}";

data _null_;

set WORK.temp_report;
length printLine $ 900;


printLine = catx(" ",'Area: ', Region,' -> ', 'Country: ',Country , ' -> ', 'Account: ', Account);
rc = dosubl(cats("ods pdf text=", quote(strip(printLine)), ";") );

printLine = catx(" ",'Ammount: ', put(Amount,dollar15.2) );
rc = dosubl(cats("ods pdf text=", quote(strip(printLine)), ";") );


printLine = catx(" ", 'Date Started: ', put(Delivered, date9.));
rc = dosubl(cats("ods pdf text=", quote(strip(printLine)), ";") );

printLine = catx(" ", Last_Update_Text);
rc = dosubl(cats("ods pdf text=", quote(strip(printLine)), ";") );

printLine = catx(" ",'--------------------------------------');
rc = dosubl(cats("ods pdf text=", quote(strip(printLine)), ";") );

run;
ods pdf close;

 

 

Super User
Super User
Posts: 7,720

Re: ODS PDF Report - Output 10 Lines to one PDF in a non-table format

It seems like you are learning SAS, there are some basics you might want to look at.  Datasteps are manipulating data, output procedures such as report and print are for creating output.  So your problem resolves to:

1) Create a dataset which looks like the output should.

2) Print that dataset

 

So:

data want;
  set have;
  length text_to_print $200;
/* Catx means concatenate each of these text strings with space delimiter between */ text_to_print=catx(' ',Person number,put(var1,best.),'Lives in',var3,'and has',put(var5,best.),'children.'); run; ods pdf file="...\<filename>.pdf"; proc print data=want noobs; run; odf pdf close;
Contributor
Posts: 23

Re: ODS PDF Report - Output 10 Lines to one PDF in a non-table format

Thanks! I've resolved the format issue I had thanks to your suggestion. I've added: put(var1,dollar15.2)

Appreciate your assistance Smiley Happy
Super User
Posts: 9,874

Re: ODS PDF Report - Output 10 Lines to one PDF in a non-table format

Actually you can do it with proc report.


data have;
 set sashelp.class;
 n+1;
run;

ods pdf file='/folders/myfolders/x.pdf';
proc report data=have nowd style={rules=none frame=void};
column n name age ;
define n/order noprint;
define name/order noprint;
define age/order noprint;

compute after age;
 temp=catx(' ','Person number ' ,n ,'Lives in' ,name,' and has ' ,age ,' childerns.');
 line temp $100.;
endcomp;
run;
ods pdf close;


☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 579 views
  • 2 likes
  • 5 in conversation