Hey guys,
I am trying to split a string variable by its delimiter and printing the whole thing into 2 different SAS tables on 2 Pages as a PDF. Here is some example data:
data mydata;
length Info1 $20 Info2 $100;
Info1 = "Content";
Info2 = "Houses in Germany; Houses in Turkey";
output;
run;
So far, I have tried to split the Info2 Variable by a semicolon like that:
%let x = 1;
ods pdf file="J:\Allgemein\Skalendoku23\Ordnerstruktur SkaD\E\Skalendoku.pdf";
ods pdf startpage=now;
ods layout start;
data want;
set mydata;
Item = scan(Info2, &x, ";");
run;
proc report data=want;
run;
ods pdf startpage=now;
ods pdf close;
ods layout end;
The code above produces a table with a new variable called "item" and stores the second string behind the semicolon (= Houses in Turkey) in it. Unfortunately, I am struggling with implementing that logic into a DO-Loop so that I would generate 2 Pages in a PDF Document, where the result should look like that on Page 1:
and like that on Page 2:
Is it possible to create some kind of DO-Loop that extracts the wanted string out of the info2 column and write it into a separate table at once? As I want to do that for about 200 different Items (lets say "countries"), I thought about creating a macrovariable that stores all countries and produces 200 tables (which would equal 200 Pages in the PDF Document); one for each country. The Information in Info1 (= Content) should stay the same. I am happy for any advice!
Kind regards
I have re-confirmed that the code I wrote works properly. The code you show includes code that you have changed from my code. Why did you change my code (other than the filename= in my code, which has to be different on my computer)? What is wrong with using the code EXACTLY as I wrote it (other than filename= )?
Also, please read the log carefully (Maxim 2) when your code doesn't work. It says:
NOTE: Variable Info_2 is uninitialized.
so you are trying to work with a variable named Info_2 which does not exist.
You need a loop inside a macro to create the PDF. However, you only want ODS PDF FILE= once (in other words not in the loop) and you only want ODS PDF CLOSE once (in other words not in the loop).
%macro do_this;
ods pdf file="Skalendoku.pdf";
%do x = 1 %to 2;
ods pdf startpage=now;
data want;
set mydata;
Item = scan(Info2, &x, ";");
run;
proc report data=want;
run;
%end;
ods pdf close;
%mend;
%do_this
Then you say
Is it possible to create some kind of DO-Loop that extracts the wanted string out of the info2 column and write it into a separate table at once? As I want to do that for about 200 different Items (lets say "countries"), I thought about creating a macrovariable that stores all countries and produces 200 tables (which would equal 200 Pages in the PDF Document); one for each country. The Information in Info1 (= Content) should stay the same. I am happy for any advice!
I am not sure I can derive any meaning from this, nor do I see how this corresponds to the example you have presented. Instead of talking about macro variables, just explain the input and explain the desired output (don't try to explain the code).
Hey, thank you for your reply!
As long as I understood you correctly my code should now look like this:
data mydata;
length Info1 $20 Info2 $100;
Info1 = "Content";
Info2 = "Houses in Germany; Houses in Turkey";
output;
run;
%macro do_this;
data have;
set mydata;
run;
ods pdf file="J:\Allgemein\Skalendoku23\Ordnerstruktur SkaD\E\Test.pdf";
%do x = 1 %to 2;
ods pdf startpage=now;
ods layout start;
data want;
set have;
Item = scan(Info_2, &x, ";");
run;
proc report data=want;
run;
%end;
ods pdf close;
ods layout end;
%mend do_this;
%do_this;
I agree that my explanation about macrovariables is not quite understandable and I think it will be enough if I understand how to do the macro correctly. Unfortunately, with the code above I now create a PDF that looks like this:
I get one page out of the code which is quite confusing to me as I would have thought that the macro should generate 2 pages atleast due to the do-loop. To me the code looks like it should do what its supposed to, but obviously it does not. However, I am still not getting 2 pages out and it does not read the information out of my data and writes it into separate tables (as in my original question). Do you know where I went wrong?
I have re-confirmed that the code I wrote works properly. The code you show includes code that you have changed from my code. Why did you change my code (other than the filename= in my code, which has to be different on my computer)? What is wrong with using the code EXACTLY as I wrote it (other than filename= )?
Also, please read the log carefully (Maxim 2) when your code doesn't work. It says:
NOTE: Variable Info_2 is uninitialized.
so you are trying to work with a variable named Info_2 which does not exist.
Alright, thank you! I have overseen the Info_2 message and thanks for the maxim link. Furthermore, I would have liked to work with the ods layout command but apparently that will stop SAS from generating 2 pages. Anyway, thank you for your time 🙂
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.