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

Hello,

(I should preface this with I am a self taught programmer that has moved from VBA to SQL to ActionScript to .net and now to SAS as my work has required. I have no formal training in programing, so most of what I've done is based on how I've learned to do it hunting through the internet... many times I find that what I want to do is easy, but I'm not great at articulating to Google what I'm trying to do )

 

 I am using Enterprise Guide 7.15 HF2 64bit

 

I have a large set of data, but I want to create PDFs of charts from the  data that are specific to certain subsets of the data. These graphical outputs are generally the same (same layout, same design, but different data, different labels, and different colors). These outputs are also going to potentially change over time since we are building a new reporting suite and will be going through a test and adjust period with my clients.

 

In other coding languages, I would set a few parameters, pass them into the "main" code, output the pdf, and repeat.

 

I tried this by setting up four short programs with code like the following

%let WhichSite = 'WAK';
%let LightColor=vigy;
%let DarkColor=Green;

or

 

%let WhichSite = 'WST';
%let LightColor= LIOY ;
%let DarkColor=GOLD;

 

I then had another larger program that contains 31 separate PROCS (8 PROC SQL, 9 PROC SGPLOT, 12 PROC GSLIDES, 2 PROC GREPLAY)  to create the PDF outputs based upon the three macro variables I pass in.

 

If I manually run the code I can get the output I want. But I can't add the larger code group to the ordered list more than once. Likewise, If I set up multiple ordered list, one for each site (set the variables, run the main code, export the pdf) it will only do it for the first data set.

 

When I did my search for this topic I saw "use macro programing to loop". But the examples all seemed to be related to creating data sets, I couldn't understand how to tell the macro to run PRG_WAK_SETTINGS followed by PRG_GRAPHIC_TDO followed by WST_SETTINGS followed by PRG_GRAPHIC_TDO ... etc...

 

Any help would be appreciated. Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Your really not making it clear here either.  A macro looks like this:

/* The definition */

%macro name (whichsite=,lightcolor=,darkcolor=);
  ...some sas procs/datasteps
%mend name;

/* The actual call */

%name (whicsite=WAK,lightcolor=vigy,darkcolor=gold);

But if you just want the same report with different by groups and templates, then its simpler to setup the data correctly, use by groups, and apply the right template.  This is a large topic, and if you don't really know much SAS much be a stretch.  

In your data, just set a new variable to show which output the row will go on, then you can use that filer, and a by.  You can have loads of different templates stating colors and such like and just switch them each run.  As I say, its a large topic, but you can find loads of information out there on:

proc report with by group  <- produce reports on by groups

proc sgplot with by group  <- produce graphs on by groups

proc template and sgrender   <- create complex graphs with dynamic variables

proc template style   <- create style templates

 

View solution in original post

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Your really not making it clear here either.  A macro looks like this:

/* The definition */

%macro name (whichsite=,lightcolor=,darkcolor=);
  ...some sas procs/datasteps
%mend name;

/* The actual call */

%name (whicsite=WAK,lightcolor=vigy,darkcolor=gold);

But if you just want the same report with different by groups and templates, then its simpler to setup the data correctly, use by groups, and apply the right template.  This is a large topic, and if you don't really know much SAS much be a stretch.  

In your data, just set a new variable to show which output the row will go on, then you can use that filer, and a by.  You can have loads of different templates stating colors and such like and just switch them each run.  As I say, its a large topic, but you can find loads of information out there on:

proc report with by group  <- produce reports on by groups

proc sgplot with by group  <- produce graphs on by groups

proc template and sgrender   <- create complex graphs with dynamic variables

proc template style   <- create style templates

 

Puertorekinsam
Fluorite | Level 6

Sorry,

 

I did not include any examples of where I tried it as a Macro because I wasn't finding a solution that seemed to fit my needs.

 

The first mistake I was making was trying to define a Macro in one program, and call it from another, and the second was I did not realize I could put the %Macro %mend tags around multiple PROCS and Data steps, I thought I had to define them separately and call them individually.

 

What you showed me here  was exactly what I needed:

 

  ...some sas procs/datasteps

 

I thought I was doing something wrong because the PROC statements were not formatting 'normally' in the macro.

 


Thank you! 

ballardw
Super User

Macros are one way to generate code.

 

Another, which is often more flexible for certain tasks (such as expanding lists of variable to have the process) is to have the names of the variables in a data set that is used via the Call Execute statement to generate the code.

A brief example:

 

   length dataset $43 variable $32;
   input dataset variable;
datalines ;
sashelp.class age
sashelp.class height
sashelp.cars  cylinders
;
run;

data _null_;
   set work.control;
   call execute('proc freq data='||dataset||';');
   call execute('tables '||variable||';');
   call execute('run;');
   call execute('proc sgplot data='||dataset||';');
   call execute('histogram '||variable||';');
   call execute('run;');
run;

Longer "boilerplate" code such as long common option lists could be in a single call execute bit.

 

This approach is a bit more flexible as you could include a variable in the control set to say which what type of plot to execute and test for the value of the variable to create different lists in a manner that is often much easier than the comparisons in macro code.

You can use conditions to create long string variables to hold a line of code text and then call execute(stringvar) as long as it will resolve to proper code in order.

You could even have the ODS PDF statement(s) generated conditionally.

You can use this approach to send parameters to a macro but there are some timing issues that may occur and you likely would not place the macro call in double quotes as you want the macro call to resolve after the data step finishes.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2085 views
  • 1 like
  • 3 in conversation