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 folks,

 

I am interested in writing a do loop, that uses information out of a SAS Table and puts that information together with a .jpg into a pdf. The data I am working with looks more or less like this:

ExSASTable.PNG

Now, I would like to extract all rows one by one in the table and merge it together with a .jpg. So the outcome should look like the attached pdf. I also attached some example data in an xlsx file. The code I have got so far looks like this:

%let i = 1;
%do i = 1 to 3;
%let as = Var1 Var2 Var3;
data reduced;
set ExEXc;
if item = "&as"; 
run;

ods pdf file="J:\Learning SAS\Test.pdf";
ods layout start width = 19cm height = 28cm ; 
ods region y=3cm x=0.4cm;
proc report data=reduced;
run;
ods region y=6cm x=3cm width=13cm height=8cm style={backgroundimage="J:/Variables/&as.jpg" };
ods text= " ";
ods layout end;
ods pdf close;
%let i = &i + 1;
%end;

As you can see in the second ods region statement, I am refering to a folder that contains the .jpg files thats looks like this:

Example Pic.PNG

Ideally, my &as macro Variable contains the Variables and the .jpg files altogether so the code is a as simple as possible. I am not able to attach .jpg files, but it really doesnt matter which pictures are used as long as they got the same name as the Variables.

 

To conclude: As I am just getting used to macro coding, I was wondering if any of you knows how to specify the DO Loop, so that it reads the first line of my data and merges it together with the matching first .jpg file, puts it together on page one of the pdf and continues with selecting the next line of the data, the next .jpg file, puts it on the second page of the pdf and so on.

 

Kind regards,

 

Jakob

1 ACCEPTED SOLUTION

Accepted Solutions
_Manhattan
Quartz | Level 8
Alright, I have found something that works for me. I simply put an macro variable (%Let Variable) and then use the scan function, to read the variables one by one :
%Let Var1to100 = Var1 Var2 ... Var100
%do i % to 100)
%Let Variable = SCAN(&Var1to100, &i)

View solution in original post

3 REPLIES 3
bnawrocki
Quartz | Level 8

Here's something that worked for me. Part of your issue was that you weren't putting your code inside a %macro/%mend. I created a macro I called "doSomething" within %macro/%mend statements. I only used 2 variable values: Var1 and Var2, but I think you get the general idea, I hope.

data ExEXc;
  input Variable $ Mean SD;
  datalines;
  Var1 2 0.67
  Var2 3 0.43
; run;

%macro doSomething();
	%let Var1 = Var1;
	%let Var2 = Var2;
	%let Pic1 = c:\temp\pic1.jpg;
	%let Pic2 = c:\temp\pic2.jpg;
	
	ods pdf file="C:\temp\Test.pdf" notoc startpage=no;

	%do i=1 %to 2;
		ods layout start width = 19cm height = 28cm ; 
		ods region y=3cm x=0.4cm;
		proc report data=ExEXc;
			where variable = "&&Var&i";
		run;
		ods region y=6cm x=3cm width=13cm height=8cm style={backgroundimage="&&Pic&i" };
		ods text= " ";
		ods layout end;
		ods startpage=now;
	%end;
	
	ods pdf close;

%mend;

%doSomething();
_Manhattan
Quartz | Level 8
Hey bnawrocki,

thank you a lot for your help! It somehow works for me, but not quite as intended. I guess the main difficulty lays in automatising the variables and pictures. Within your example for e.g. 100 Variables and 100 Pics, I would need to define all of them in the %let Statements wouldn't I?
So it would look something like this:
%let var1 = var1
%let var2 = var2
...
%let var100 = var100
and the same with the pics:
%let pic1 = pic1.jpg
%let pic2.jpg = pic2.jpg
...
%let pic100 = pic100.jpg

I would like to include all of them in one let statement. Furthermore, the variables and pics dont always have the structure of var"1" pic"1" etc. they look more like "XYZ3461" "XYZ2345". So I am not even sure, if the whole Do Loop with i=1 to X works for me. I do not know if there is any other way of doing this. So far I have not come up with anything else. Do you have any Idea?

Kind regards,

Jakob
_Manhattan
Quartz | Level 8
Alright, I have found something that works for me. I simply put an macro variable (%Let Variable) and then use the scan function, to read the variables one by one :
%Let Var1to100 = Var1 Var2 ... Var100
%do i % to 100)
%Let Variable = SCAN(&Var1to100, &i)

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
  • 3 replies
  • 917 views
  • 1 like
  • 2 in conversation