BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
_Manhattan
Quartz | Level 8

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:

Jakobsen96_0-1683725805539.png

and like that on Page 2:

Jakobsen96_1-1683725828624.png

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

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).

 

 

--
Paige Miller
_Manhattan
Quartz | Level 8

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:

Jakobsen96_0-1684229265673.png

 

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?

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
_Manhattan
Quartz | Level 8

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 🙂

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 535 views
  • 1 like
  • 2 in conversation