BookmarkSubscribeRSS Feed
tuncay
Calcite | Level 5

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

4 REPLIES 4
Astounding
PROC Star

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.

tuncay
Calcite | Level 5

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.

ballardw
Super User

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.

tuncay
Calcite | Level 5

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 4 replies
  • 1814 views
  • 0 likes
  • 3 in conversation