BookmarkSubscribeRSS Feed
helloagainoh2
Calcite | Level 5

I am wondering if this is possible in SAS. I am working in enterprise and noticed that I can schedule for projects to be ran, I currently run a program once a week that only requires me to update a variable by 1.

 

I would like to automate this if possible, how can I write code to update this variable every time it is scheduled to run? In another language like python I would write some code like this. Is there a way to do this in SAS?

 

 

week_num = 1

temp_inc = 1

first_time_running = True

 

if first_time_running = False then temp_inc = temp_inc + 1

 

if first_time_running = True  then week_num = 1 and first_time_running = False

else week_num = temp_inc

 

 

data test;

set test_2;

use week_num;

run;

 

 

 

 

 

3 REPLIES 3
PaigeMiller
Diamond | Level 26

I would create a permanent SAS data set (which I will name mylib.dsname) with only one variable (let's call it BOB) and only one observation. The first time you run it, the data set is created and BOB gets the value of 1. Then next time you run it, BOB has the value of 2, and so on.

 

libname mylib "somevalidlocation";
%if %sysfunc(exist(mylib.dsname)) %then %do;
data mylib.dsname;
    set mylib.dsname;
    bob=bob+1;
run;
%end;
%else %do;
data mylib.dsname;
    bob=1;
run;
%end;
  

Note: works in SAS 9.4M5 or later.

--
Paige Miller
Reeza
Super User
Where do you store your data?
In that location, create a table with the iteration run. When you finish a run, update that table so that there's a new record. At the start of the process you would read that record to get the newest value to work with.

Untested general idea, assuming you also understand the concept of macro variables.

*start of process;
proc sql noprint;
select max(iteration)+1 into :next_iter from iteration_count;
quit;

/*process uses &next_iter value*/



/*end of process*;
proc sq;
insert into iteration_count
select &max_iter +1;
quit;

And just as an FYI - if this iteration revolves around dates then there are better ways to do this.
Patrick
Opal | Level 21

If you just need some indication in which week you've executed a job then why not just store the execution date as a SAS date value. This would avoid the need to maintain some permanent table for maintaining some execution counter ...and the additional logic required to not increase this counter when re-running in the same week because something went wrong the first time.

 

If you just store the date as a SAS Date value then you can always use SAS functions like intck() or week() to derive week counts.

 

Below some sample code showing options.

%let first_run_dt='1jan2020'd;
data test;
  set sashelp.class;
  week_num_1=intck('week',&first_run_dt,today());
  week_num_2=week(today());
  yyww_num=input(cats(year(today()),put(week(today()),z2.)),16.);
  dt=today();
  format dt weekv.;
run;

Patrick_0-1640835961777.png

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 1053 views
  • 0 likes
  • 4 in conversation