Greetings,
1. I have a 10X5 dataset. For example:
Var1 | Var2 | Var3 | Var4 | Var5 |
1 | David | US | 5 | 1 |
2 | Brad | Canada | 87 | 2 |
3 | Knight | Canada | 5 | 4 |
4 | Bill | Canada | 6 | 5 |
5 | Donald | US | 64 | 7 |
6 | Hillary | US | 4 | 98 |
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 1 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
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;
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.
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;
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
@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.
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
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;
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;
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.