BookmarkSubscribeRSS Feed
kajal_30
Quartz | Level 8

I have a sas program which is using date input and I want to run that program iteratively for collecting historical data on the bases of different dates. May I please know how can I run that program repetitively? 

data dtmt;
	length longstr1 $ 200;

	Do i= 1 to countw("&yearmonth2.");
		dttemp = scan("&yearmonth2.",i);
		call symputx (yrmnth,scan("&yearmonth2.",i));
		longstr1 = (abc.sas);
		output;
		call execute(longstr1);
	end;
run;

unable to create it as a macro as there are multiple steps and may need lots of adjustments. preferring to run .sas program again n again.

 

Thanks

Kajal

6 REPLIES 6
PaigeMiller
Diamond | Level 26

unable to create it as a macro as there are multiple steps and may need lots of adjustments. 

 

Macros can have multiple steps and lots of adjustments. So you need to explain this a lot more.

 

But I guess I don't understand the larger problem. You already have code that can have different dates, just change the value of &yearmonth2. So you need to explain what you want in a lot more detail.

--
Paige Miller
ballardw
Super User

You are very likely having timing issues between the creation of the macro variables, when they are available and when the call execute attempts.

Instead of "call execute" with macro variables try writing to a program file and then call that.

The output of that would create a program file that looks something like (dummy code guessing what you want)

%let yrmonth=(some value);
%include abc.sas;
%let yrmonth=(a different value);
%include abc.sas;

Caution: %include should reference a complete path from a drive or disk mount point to start so SAS knows where to look for the file.

 

The code you posted doesn't even come close to running because of the errors generated by syntax error in your attempted assignment to Longstr1:

95     longstr1 = (abc.sas);
                   -------
                   557
ERROR: DATA STEP Component Object failure.  Aborted during the COMPILATION phase.
ERROR 557-185: Variable abc is not an object.

 

 

What does the apparent code in abc.sas contain? If it is pretty much constant code that attempts to use the value of the macro variables Yrmonth then likely it could be rewritten so that the text of Yrmonth is a PARAMETER to a macro.

 

Placing those values into a macro variable may have just complicated your whole process. If they had been in a data set, one value per observation then

 

Tom
Super User Tom
Super User

Don't put the list of dates into a macro variable, leave them in a dataset.

Use the dataset to generate the code you want to run.

 

So if you have a dataset named HAVE with a variable named YEARMONTH that has values you can use to create the macro variable then just use that dataset to generate the code you want to run.

filename code temp;
data _null_;
  set have;
  file code;
  put '%let ' yearmonth= ';' 
    / '%include "filename.sas";' 
  ;
run;
%include code / source2;

 

So that will run the program file repeatedly. 

Iteratively has a different meaning that might imply the results of earlier runs impact the later runs.  If you need that then you need to explain in more detail what the code in filename.sas is actually doing.

Reeza
Super User

You're using CALL EXECUTE in your code. 

Is there any particular reason you couldn't replicate the full process leveraging CALL EXECUTE?

 

 

Kurt_Bremser
Super User

Multiple severe mistakes here which have nothing to do with macro code, but with missing familiarity with the most basic SAS concepts.

call symputx (yrmnth,scan("&yearmonth2.",i));

The arguments to CALL SYMPUTX have to be character expressions, so SAS goes looking for a variable yrmnth, which you never create in your step, so you get a missing value there.

You probably wanted

call symputx ("yrmnth",scan("&yearmonth2.",i));
longstr1 = (abc.sas);

Since no complicated caculations are attempted here, the brackets serve no purpose, so omit them.

Once again, it looks like you missed to use quotes, which dupes SAS to think you wanted to use a component object (which is nowhere defined).

call execute(longstr1);

CALL EXECUTE is used to push SAS code to the execution queue, whuch willbe interpreted imnediately after the current step has finished. Therefore the argument must be a character expression containing valid SAS code.

"abc.sas" is not valid SAS code.

 

Maxim 1: Read the Documentation, so you know what the tools you intend to use do, and how you have to use them.

 

Before you can run, you need to have mastered walking, which is usually preceded by crawling. So let's start with crawling:

Go back to square 1: you say you want to run a certain SAS program repeatedly, feeding it a date parameter. How would that code look like for the first two dates, without any automation?

Reeza
Super User

First you must have working code. Then you generalize/iterate it. If you have working code can you share that and show how it would change for a second run?


I think I linked you to this reference previously, but it seems to apply again. 

https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md

 

 

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
  • 6 replies
  • 482 views
  • 0 likes
  • 6 in conversation