BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Kurt_Bremser
Super User

You are mixing macro and data step code.

%if is macro code, but the month() and year() functions are data step functions and will be treated by the macro processor as plain text(!), so you get the condition (quotes for emphasis) "month(today)" ge "2", which is always true (the character "m" has a higher ordinal than the character "2").

I guess that you get complaints in your log about

%let mois2 = %eval(month(today) - 1);

like "operator not found in expression".

I suggest to put your condition evaluation into a data _null_ step and, when you have verified that the data step activates the right branch (use a simple put "does this"; and put "does that"; to check), replace the put with a call execute to call the macro.

fadel
Calcite | Level 5

but the problem is not resolved yet !! the programm will be executed more than one time !

jakarman
Barite | Level 11

Make your approach more simple to understand and code.

There is a format that gives you immediate yyyymm values 

http://support.sas.com/documentation/cdl/en/leforinforref/63324/HTML/default/viewer.htm#n1k45hxg0vxo...


options mprint mlogic symbolgen source2 ;

    %let today=%sysfunc(today());                                    

    %let fstmonth=%sysfunc(Putn(&today,yymmn6.) ) ;
    %put &fstmonth ;      /* shows the yyyymm value you are needing  */

%macro kpisgenerals (mois1=,annee1=);   /* your code as  macro set up as dummy, just checking dates */
  %put -- &mois1 --   &annee1  -- ;  
%mend;

   /* I do not have f DATAMART.DM4_DATAMART&fstmonth instead of that test for    test.have  */

   /* The test you forgot is marking the run has finished. I create a data set for that  with a lastrun naming.

       It will execute you macro only once a month no matter how many times you call checkrun ... shifting the days as for namings using the %eval  as you need  */

%macro checkrun;
    %if (%sysfunc(exist(test.have ) ) And not %sysfunc(exist(test.lastrun_DM4_DATAMART&fstmonth) )  )  %then %do;
     %kpisgenerals(mois1=%substr(&fstmonth,5,2), annee1 = %substr(&fstmonth,1,4)); /*my macro */
   data test.lastrun_DM4_DATAMART&fstmonth ;  /* mark job has run  */
     today=today(); format today date. ;
   run;
    %end ; 
%mend;
   
%checkrun;
   

---->-- ja karman --<-----
fadel
Calcite | Level 5

Hi, Thanks it works with this code:

filename mylib "/home/sassrv/user_folder/sasuser/adnane/projet_tdb_mustapha/myautocall";

options mrecall;

options mautosource sasautos=(mylib sasautos)  mautolocdisplay;

%macro rrr();

%if (%sysfunc(month("&sysdate"d)) ge 2 & %sysfunc(month("&sysdate"d)) le 12) %then %do;

%let fdlmonth = %sysfunc(month("&sysdate"d));

%let mois2 = %eval(&fdlmonth - 2);

%let annee2 = %sysfunc(year("&sysdate"d));

%let fdlyear = %sysfunc(year("&sysdate"d));

%end;

%else %do;

%let mois2 = 12;

%let fdlyear = %sysfunc(year("&sysdate"d));

%let annee2 = %eval(&fdlyear - 1);

%end;

%if ( &mois2 lt 10) %then %do;

%let mois2 = 0&mois2;

%end;

%if (%sysfunc(exist(DATAM.DM4_DATAMART_&annee2&mois2)) And not %sysfunc(exist(sasuser.lastrun_&annee2_&mois2) )) %then %do;

   %kpisgenerals(mois1= &fdlmonth, annee1= &fdlyear);

   data sasuser.lastrun_&annee2_&mois2 ;  /* mark job has run  */

      today=today(); format today date. ;

   run;

%end;

%mend;

%rrr()

Now i would like to schedule itin gaol be executed once a day !! (im working in SAS EG 4.3) Can you inform me about that step by step ?

fadel
Calcite | Level 5

the question: is how to schedule this programm in sas eg 4.3?

Kurt_Bremser
Super User

I would not schedule the code via EG at all, but as a SAS batch job with a shell script, either for bash or ksh in UNIX or Powershell in Windows. And then run that shell script daily via the operating system's scheduling tool, in UNIX this is the cron daemon.

If your SAS server is UNIX it looks like that:

In EG, save your code in Files on the App Server as batchjob.sas

Create a file named batchjob.sh in your home directory that contains this:

#!/usr/bin/ksh

sas $HOME/batchjob.sas -log $HOME/batchjob.log

If the path to your SAS executable is not in the $PATH variable, you may have to specify the complete path instead of just "sas"

Make this file executable by running "chmod u+x $HOME/batchjob.sh" once.

Then create a note containing the follwing line in EG and save this to Files (as above) named crontab.txt:

30 23 * * * $HOME/batchjob.sh

Then log on to the server and do

crontab crontab.txt

From then on, the script will be run daily at 2330 hours. You can view the log in the Files section in EG.

fadel
Calcite | Level 5

thank you :smileygrin:

fadel
Calcite | Level 5

Last question: i don't have access to the terminal of server to rannig comands, there is an other way (in the EG 4.3 Interace) or should i contact my admin?

jakarman
Barite | Level 11

Ask you admin and local IT-support guys. When they are cooperative it should be a quick fit.

When they are not cooperative you still have the option to build something yourself although it should not be that way.

There are companies selling their products and the sales is promoting as big advantage not IT involvement needed. It is a trigger for not getting a cooperative approach. 

---->-- ja karman --<-----

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 25 replies
  • 1922 views
  • 8 likes
  • 3 in conversation