DATA Step, Macro, Functions and more

Auto increment and save a field in SAS

Reply
Occasional Contributor
Posts: 7

Auto increment and save a field in SAS

Hi everyone,

I have a question that I couldn't find the solution after much looking around.

I am trying to convert a VBA macro (everything done, except this part) which runs sqlplusw to extract data, copy into excel worksheet and saves it. In this macro the header line has a run number, and this run number is incremented one each time we run it.

Is there a function in SAS that I can start from a definite number and increment the run number by itself each time I run it?

I'd like to get a line something like below:

H,ddmmyyy,runnumber

Sorry if I'm not that clear, I'm a beginner in SAS.

Thanks

Fatih

Super User
Posts: 5,074

Re: Auto increment and save a field in SAS

Fatih,

Well, I need to imagine a bit how you are going to be using this.  Is it going to be in a DATA step or in a macro, for example?  As long as your run numbers are integers, both the DATA step and macro language would support any of these loops:

do n=1 to number_of_files;

  new_runnumber = runnumber * n;

end;

runnumber = runnumber + runnumber;

runnumber = runnumber + 1;

You might be looking for this combination:

%global new_runnumber;

%let new_runnumber = %eval(&new_runnumber +&runnumber);

You would have to provide some more detail about your SAS application if you need help picking out the right approach.

Good luck.

Occasional Contributor
Posts: 7

Re: Auto increment and save a field in SAS

Thanks for your help Astounding.

I haven't decided if this will be a macro or DATA step. all I want to produce is

H,ddmmyyyy,runnumber. Then I have to add this line to the top of the other dataset I produced with a simple PROC SQL, then a trailer line at the end (again simple one line dataset created with PROC SQL).


I just want to know how to create a runnumber variable which increments itself each time I run this in SAS EG, so I can add that to the header line.


If I'm not clear enough, please let me know; I'm new to this area.


Thanks again.

Super User
Posts: 10,466

Re: Auto increment and save a field in SAS

Is this to keep track of runs across SAS sessions or within a single session?

If there doesn't need to be any persistence across sessions the easiest would be to have a piece of code that only runs once, say in a SAS Autoexec.sas file, like :

%let RunNumber=0;

Then at the top of the code you need:

%let RunNumber = %eval(&RunNumber +1);
%put &RunNumber; /* to see output in log while testing*/

%let Header= H,%sysfunc(putn("&sysdate"d,ddmmyyn8.)),&RunNumber;

%put &Header; /* to see output while testing*/

The Header would be referenced if file names similar to

filename outfile "C:\thisfolder\&header..txt";  /*note the first . is to concatenate with the .txt part of file name*/

or in atitle statement

title3 "Run: &Header";

Or "&Header" most places where the text is needed

If it needs to persist across sessions things get a tad trickier.

Occasional Contributor
Posts: 7

Re: Auto increment and save a field in SAS

Hi ballardw,

This is to keep track of runs across sessions. If I cannot find a solution, we will just manually change the number each time we run this code.

It's interesting that such an easy thing in VBA (which I have very very limited knowledge of; but looking at previous reports I can decipher it) is too hard to accomplish in a powerful program like SAS.

Thanks

Ask a Question
Discussion stats
  • 4 replies
  • 753 views
  • 0 likes
  • 3 in conversation