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

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
BrunoMueller
SAS Super FREQ

Hi

 

As @Kurt_Bremser 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

9 REPLIES 9
Kurt_Bremser
Super User

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.

BrunoMueller
SAS Super FREQ

Hi

 

As @Kurt_Bremser 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;

 

DanielDor
Obsidian | Level 7

Greetings,

 

Thanks for your prompt response! 🙂 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

Kurt_Bremser
Super User

@DanielDor wrote:

Greetings,

 

Thanks for your prompt response! 🙂 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.

BrunoMueller
SAS Super FREQ

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

DanielDor
Obsidian | Level 7

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;

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
DanielDor
Obsidian | Level 7
Thanks! I've resolved the format issue I had thanks to your suggestion. I've added: put(var1,dollar15.2)

Appreciate your assistance 🙂
Ksharp
Super User
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;


sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 9 replies
  • 2057 views
  • 2 likes
  • 5 in conversation