BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sasnewbiexx
Calcite | Level 5

Hello everyone,

 

Basically, I'm trying to create the following macro:

 


%Macro CheckDate;

 

%if &month= this month %then %do;

 

          Proc Sql;
          Create table Work.Test1 as
          Select A.*
          From Work.Table1 
          ;
          Quit;

%End;

 

%Else %do;

 

signoff

 

%End;
%Mend;
%CheckDate;

 

 

I am trying to get my month macrovariable to be the month part for the first observation from a datetime column from another table (lets say Work.Table2). Any ideas how can I do that?

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Since you need to pull data from a separate data set to get the comparison datetime value, I would recommend using a DATA step and doing all the heavy lifting in that DATA step.  For example:

 

%macro CheckDate;
   
   %local proceed;
   %let proceed=N;
   data _null_;
      set other_table (keep=last_update_datetime);
      if month(today()) ne month(datepart(last_update_datetime)) then
      call symput('proceed', 'Y');
      stop;
   run;
   %if &proceed=Y %then %do;
      ...
   %end;
    %else %do;
         * signoff logic;
    %end;

%mend CheckDate;

Of course, I had to invent names for your data sets and variables.  And I assumed that your datetime source is actually a numeric SAS datetime, not a character string.  All of that can be tweaked if necessary.

View solution in original post

5 REPLIES 5
Astounding
PROC Star

Here's the sort of information you will need to supply to approach this problem.

 

What is in that datetime value from the other table?  Is it a character string?  Is it a SAS numeric datetime?

 

How do you intend to populate "this month"?  Is it something you feed in as a parameter?  (If so, is it a number from 1 to 12, or a three-letter abbreviation, or a full word?)  Is it the month during which the program runs?

 

Is it always sufficient to check just the month, so you will never need to check the year as well?

 

 

 

 

sasnewbiexx
Calcite | Level 5

@Astounding 

The datetime format from the other table is DATETIME22.3 (11APR2019:10:27:15.780).

 

My apologizes, when I say "this month", I meant to say the current month's value. I intend to take from SAS, but I'm not sure how in this case given the format of the date value from the other table.

 

The table that I take the date from gets updated once a month, but the DBA told me that it is not on a prefixed date so it can be in the beginning of the month or the end. Therefore, my idea is to make my program execute only if the month value is different than the current month's value and just skip to signoff if the month values match. 

 

I wouldn't need to check the year since the data is updated every month and all the values in the datetime column I take are from the same day.

Reeza
Super User

TODAY() returns todays date

MONTH() retrieves the month from a SAS date.

To use functions within macro logic, wrap them in %SYSFUNC().

 

Not really sure where the other month value is coming from. 

 

%if %sysfunc(today(), month.) = 5 %then %do;


@sasnewbiexx wrote:

Hello everyone,

 

Basically, I'm trying to create the following macro:

 


%Macro CheckDate;

 

%if &month= this month %then %do;

 

          Proc Sql;
          Create table Work.Test1 as
          Select A.*
          From Work.Table1 
          ;
          Quit;

%End;

 

%Else %do;

 

signoff

 

%End;
%Mend;
%CheckDate;

 

 

I am trying to get my month macrovariable to be the month part for the first observation from a datetime column from another table (lets say Work.Table2). Any ideas how can I do that?


 

Astounding
PROC Star

Since you need to pull data from a separate data set to get the comparison datetime value, I would recommend using a DATA step and doing all the heavy lifting in that DATA step.  For example:

 

%macro CheckDate;
   
   %local proceed;
   %let proceed=N;
   data _null_;
      set other_table (keep=last_update_datetime);
      if month(today()) ne month(datepart(last_update_datetime)) then
      call symput('proceed', 'Y');
      stop;
   run;
   %if &proceed=Y %then %do;
      ...
   %end;
    %else %do;
         * signoff logic;
    %end;

%mend CheckDate;

Of course, I had to invent names for your data sets and variables.  And I assumed that your datetime source is actually a numeric SAS datetime, not a character string.  All of that can be tweaked if necessary.

sasnewbiexx
Calcite | Level 5
Thank you so much, this was exactly what I was looking for! Cheers!

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1152 views
  • 5 likes
  • 3 in conversation