DATA Step, Macro, Functions and more

SAS do loop sgplot

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 87
Accepted Solution

SAS do loop sgplot

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;

 


Accepted Solutions
Solution
‎01-26-2018 02:03 PM
Respected Advisor
Posts: 2,661

Re: SAS do loop sgplot

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


All Replies
Solution
‎01-26-2018 02:03 PM
Respected Advisor
Posts: 2,661

Re: SAS do loop sgplot

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
Frequent Contributor
Posts: 87

Re: SAS do loop sgplot

Posted in reply to PaigeMiller

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

 

Super User
Posts: 13,084

Re: SAS do loop sgplot


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.

 

 

 

Super User
Posts: 22,874

Re: SAS do loop sgplot

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;

 


 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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