DATA Step, Macro, Functions and more

Pass a list of numbers from a config to a sas program

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Pass a list of numbers from a config to a sas program

[ Edited ]

?

 

   

 


Accepted Solutions
Solution
‎12-14-2016 10:10 AM
Super User
Posts: 10,497

Re: Pass a list of numbers from a config to a sas program

Lots things related to Macro lists on this forum. Here's a skeleton

Instead of the three variables I provided before you would have one variable, maybe RepNums=5 10 in the declaration.

 

In the body of the macro;

 

%let Countrep = %sysfunc(countw(&repnums)); /* Countrep will hold the number of reports to generate*/

%do i = 1 %to &countrep;

   %let ii= %scan(&repnums,&i); /* since ii was what you have been using as the suffix*/

 

<the remainde of the code within the ii %do loop>

 

%end;

 

It is up to you to ensure something is in the Repnums varaible.

View solution in original post


All Replies
Super User
Posts: 10,497

Re: Pass a list of numbers from a config to a sas program

This looks like it should be part of a macro, a section of code that would start with %macro and end with %mend. We would need the complete text of the macro to make specific changes.

 

In general the approach would be to use keyword parameters to hold the default values and then you could call the macro with other values as needed. Here is very obviously incomplete code:

%macro dummy(<your other macro parameters go here>,fromval=5, toval=10, stepval=5)

%do __j = 1 %to &n_months;
      %put NOTE: ------------------------------------------------------;
      %put NOTE: START UPDATE FOR  &_cfgFile  Run for &&start_month&__j;
      
    %do ii = &fromval %to &toval %by &stepval;
	 
	 %let numTiles = &ii;

	 %returnSpreads( _cfgFile = &_cfgFile,
			 archive  = &archive,
			 numTiles = &numTiles,
			 startDt  = &&start_month&__j,
			 endDt    = &&end_month&__j,
			 updt     = &updt );
	
      %end;

      %let updt = 1;
   %end;
   
      %do ii = &fromval %to &toval %by &stepval;

      %put NOTE: Build reports for &strategy;
      %MakePDF( endDate   = &endDate,
		       _cfgFile  = &_cfgFile,
		       addLO     = LS,
		       numTiles  = &ii,
		       xls       = &xls);
      
      %put NOTE: Make LO tables;
      %MakePDF( endDate   = &endDate,
		       _cfgFile  = &_cfgFile,
		       addLO     = LO,
		       numTiles  = &ii ,
		       xls       = &xls);
      
   %end;
%mend;

If the DUMMY macro is called without specifying values for the fromval, toval or stepval parameters then the do loop would be the

 

Do ii = 5 %to 10 %by 5;

if you use

%Dummy (<other parameters>,fromval=3, toval=5, stepval=2);

then the loop would resolve to

Do ii = 3 %to 5 %by 2;

and hopefully works to generate your _3 and _5 reports.

NOTICE that you will have to specify an appropriate step as other wise the %by 5 would not change and things will go poorly.

Occasional Contributor
Posts: 5

Re: Pass a list of numbers from a config to a sas program

[ Edited ]

Thanks so much. 

 

 

 

 
Super User
Posts: 10,497

Re: Pass a list of numbers from a config to a sas program

The macro definition would start with something like this:

%macro build_tables ( _cfgFile  = ,
			     archive   = ,
			     startDate = ,
			     endDate   = ,
			     datesDs   = $ALPHADATA/general/datesglb,
			     xls       = ,
			     updt      = ,
                             fromval   =5, 
                             toval     =10, 
                             stepval   =5
            );

The values of fromval toval and stepval are each independent parameters. You could set them to 3 2 and 7 but that won't make sense

 

(do ii = 3 to 2 by 7). If you do not change the stepval but fromval to 3 and toval to 5 then the line generated would be do ii = 3 to 5 by 5. Then after the "3" loop executes and has 5 (the stepval) added then you get 8 and the "5" loop and report do not get generated.

And as @Astounding says this approach won't work if you need to generate report_3 report_10 report27 as a value of stepval can't be found. If you need that flexibility then you would look at providing a list of explicit values and then pulling values from that list . I suggested the approach I gave as it resulted in a minimal change to your existing code.

Occasional Contributor
Posts: 5

Re: Pass a list of numbers from a config to a sas program

[ Edited ]



Occasional Contributor
Posts: 5

Re: Pass a list of numbers from a config to a sas program

Exactly , I want to have that flexibility. I want the default to be 5 and 10. But after I specify the the values in the config, I want the program to take in those values from a list. for eg I mention num = 3 5 6 in the config. Now I want the program to pull these values from the list and pass it in a loop.. to get report_3 report_6 report_5. Not sure how to implement that. Please help.

Solution
‎12-14-2016 10:10 AM
Super User
Posts: 10,497

Re: Pass a list of numbers from a config to a sas program

Lots things related to Macro lists on this forum. Here's a skeleton

Instead of the three variables I provided before you would have one variable, maybe RepNums=5 10 in the declaration.

 

In the body of the macro;

 

%let Countrep = %sysfunc(countw(&repnums)); /* Countrep will hold the number of reports to generate*/

%do i = 1 %to &countrep;

   %let ii= %scan(&repnums,&i); /* since ii was what you have been using as the suffix*/

 

<the remainde of the code within the ii %do loop>

 

%end;

 

It is up to you to ensure something is in the Repnums varaible.

Occasional Contributor
Posts: 5

Re: Pass a list of numbers from a config to a sas program

[ Edited ]
 

Is this right?

Super User
Posts: 10,497

Re: Pass a list of numbers from a config to a sas program


malinisantra wrote:

Is this right?


You would need a parameter on the macro definition to hold REPNUMS and set the default values.

 

Since you never showed the complete definition of your existing macro I can't tell what else possibly might need to be addressed. What you show doesn't look like the original complete macro with the addition of one parameter and the lines using REPNUMS and it's values.

Super User
Posts: 5,081

Re: Pass a list of numbers from a config to a sas program

I think the reply from ballardw is looking around in the right ballpark, but it can be fairly simply implemented.  First, change the %DO loop to read:

 

%do ii=&startval %to &endval %by &byval;

 

Then just before that, define values for &STARTVAL, &ENDVAL, and &BYVAL:

 

%let _cgfFile = &_cgfFile;

 

%if "&_cgfFile" = "name of file that I want to treat differently" %then %do;

   %let startval=3;

   %let endval=5;

   %let byval=2;

%end;

%else %do;

   %let startval=5;

   %let endval=10;

   %let byval=5;

%end;

 

 

A few notes ...

 

The problem becomes more complex if you don't have a BYVAL that would hit all of the proper points along the way.  SAS macro language does not support this sort of loop:  %do ii=3, 5, 10;

 

The %LET statement that re-sets &_cgfFile is only needed if there are leading or trailing blanks as part of the value. 

 

The double quotes may not be needed, but (without the quotes) some characters that might be part of the value of &_cgfFile could potentially cause trouble if they are part of an %IF condition.

☑ This topic is SOLVED.

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

Discussion stats
  • 9 replies
  • 283 views
  • 0 likes
  • 3 in conversation