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

Hi,

I need to output a separat PDF plot file for each unit which aooear in my data set.

 

How can I make this simple and smart?????

 

The code below works but i need to loop it over the different units in the dataset so I output one plot per unit.

 

The dataset plot1 contains data for several units.

 

Hope you have some ideas.

 

Thanks in advance,

 

 

SAS program:

 

* data selection;

data plot1;

set masterdata(where=(year notin('2018') and unitname in(&unit.) )) other_sum;

run;

 

*make PDF file;

 

ods pdf file="C:/SGPLOT/&unit..pdf" notoc;

PROC SGPLOT DATA = plot1;

 

 

SERIES X = Reg_aar Y = var1/lineattrs=(color=green) ;

SERIES X = Reg_aar Y = var2 /lineattrs=(color=red) ;

SERIES X = Reg_aar Y =var3/lineattrs=(color=green pattern=dash) ;

SERIES X = Reg_aar Y =var4/lineattrs=(color=red pattern=dash);

yaxis values=(0 to 100 by 20);

 

TITLE 'Plot for &unit';

RUN;

ods pdf close;

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

You need to create a macro, and a %DO loop inside the macro.

 

Or, repeat the PROC SGPLOT code four times, and create only one SERIES statement in each repetition.

--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

You need to create a macro, and a %DO loop inside the macro.

 

Or, repeat the PROC SGPLOT code four times, and create only one SERIES statement in each repetition.

--
Paige Miller
ANLYNG
Pyrite | Level 9

This is a nice solution and it works as expected...Thanks

 

ballardw
Super User

@ANLYNG wrote:

Hi,

I need to output a separat PDF plot file for each unit which aooear in my data set.

 

How can I make this simple and smart?????

 

The code below works but i need to loop it over the different units in the dataset so I output one plot per unit.

 

The dataset plot1 contains data for several units.

 

Hope you have some ideas.

 

Thanks in advance,

 

 

SAS program:

 

* data selection;

data plot1;

set masterdata(where=(year notin('2018') and unitname in(&unit.) )) other_sum;

run;

 

*make PDF file;

 

ods pdf file="C:/SGPLOT/&unit..pdf" notoc;

PROC SGPLOT DATA = plot1;

 

 

SERIES X = Reg_aar Y = var1/lineattrs=(color=green) ;

SERIES X = Reg_aar Y = var2 /lineattrs=(color=red) ;

SERIES X = Reg_aar Y =var3/lineattrs=(color=green pattern=dash) ;

SERIES X = Reg_aar Y =var4/lineattrs=(color=red pattern=dash);

yaxis values=(0 to 100 by 20);

 

TITLE 'Plot for &unit';

RUN;

ods pdf close;

 


First would be to change the data so that you have a single Y variable and another variable to indicate group membership. Use the Group=option to point to that variable to create different lines for each value of group.

SERIES X = Reg_aar Y = value/ group=ygroupvariable;

 

When you get that working then you could either use a macro loop over your list of variables to make the individual output PDF documents. However you haven't given us a connection between "unit" and your data so can't be too specific here.

 

This forum and others have many bits of looping over variable names to create files, table or graphs either using macro code or call execute statements with a control data set containing things such as your "unit" information.

 

 

 

Reeza
Super User

If you change your SGPLOT procedure to use a BY statement you can use the NEWFILE option on the ODS PDF statement to generate a new file for each BY group. 

However the naming convention sucks, it names it Report1, Report2. 

If you want control over the file names then I think you need to use a macro. 

 

Note that your Title statement needs double quotes to work properly. If you're using BY group processing look at customizing the title using the BYVAL values. 

 

ods pdf file="C:/SGPLOT/Unit.pdf" notoc newfile=ByGroup;
PROC SGPLOT DATA = plot1;
 
By UNIT;
SERIES X = Reg_aar Y = var1/lineattrs=(color=green) ;
SERIES X = Reg_aar Y = var2 /lineattrs=(color=red) ;
SERIES X = Reg_aar Y =var3/lineattrs=(color=green pattern=dash) ;
SERIES X = Reg_aar Y =var4/lineattrs=(color=red pattern=dash);
yaxis values=(0 to 100 by 20);
 
TITLE "Plot for &unit";
RUN;
ods pdf close;

@ANLYNG wrote:

Hi,

I need to output a separat PDF plot file for each unit which aooear in my data set.

 

How can I make this simple and smart?????

 

The code below works but i need to loop it over the different units in the dataset so I output one plot per unit.

 

The dataset plot1 contains data for several units.

 

Hope you have some ideas.

 

Thanks in advance,

 

 

SAS program:

 

* data selection;

data plot1;

set masterdata(where=(year notin('2018') and unitname in(&unit.) )) other_sum;

run;

 

*make PDF file;

 

ods pdf file="C:/SGPLOT/&unit..pdf" notoc;

PROC SGPLOT DATA = plot1;

 

 

SERIES X = Reg_aar Y = var1/lineattrs=(color=green) ;

SERIES X = Reg_aar Y = var2 /lineattrs=(color=red) ;

SERIES X = Reg_aar Y =var3/lineattrs=(color=green pattern=dash) ;

SERIES X = Reg_aar Y =var4/lineattrs=(color=red pattern=dash);

yaxis values=(0 to 100 by 20);

 

TITLE 'Plot for &unit';

RUN;

ods pdf close;

 


 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2066 views
  • 0 likes
  • 4 in conversation