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

Hi, 

 

I want the the current month date to refer to the prior months start date until 4th of the current month which I am able to do using the below code.

 

data A;

format CM date9.;

if day(today())>=5 then CM=intnx('month',today(),0,'b');

else CM=intnx('month',today(),-1,'b');

 

run;

 

 

Once I get CM I want to be able to use it in another program where I will use it in the where clause 

 

proc sql;

create table WANT as select * from A(a table in my library)

where month_dt= CM;

quit;

 

If i write it this way, I get the errror that CM does not exist in A 

 

Can this be done?

OR can CM be converted to a macro  so that it can be used in WANT ?

 

Thanks a lot in advance!!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

1. Create a macro variable and use that in the query. There is no need to format the variable to appear as date9, that just makes it more complex.

data A;

format CM date9.;

if day(today())>=5 then CM=intnx('month',today(),0,'b');

else CM=intnx('month',today(),-1,'b');

 call symputx('cm', cm, 'g');

run;

%put &cm.;

 

2. Use a SQL query to select. Note that I've changed table names from A to make it less confusing. 

 

proc sql;
create table want as
select * from table1
where month_dt in (select cm from A);
quit;

@new_sas_user_4 wrote:

Hi, 

 

I want the the current month date to refer to the prior months start date until 4th of the current month which I am able to do using the below code.

 

data A;

format CM date9.;

if day(today())>=5 then CM=intnx('month',today(),0,'b');

else CM=intnx('month',today(),-1,'b');

 

run;

 

 

Once I get CM I want to be able to use it in another program where I will use it in the where clause 

 

proc sql;

create table WANT as select * from A(a table in my library)

where month_dt= CM;

quit;

 

If i write it this way, I get the errror that CM does not exist in A 

 

Can this be done?

OR can CM be converted to a macro  so that it can be used in WANT ?

 

Thanks a lot in advance!!


 

View solution in original post

5 REPLIES 5
Reeza
Super User

1. Create a macro variable and use that in the query. There is no need to format the variable to appear as date9, that just makes it more complex.

data A;

format CM date9.;

if day(today())>=5 then CM=intnx('month',today(),0,'b');

else CM=intnx('month',today(),-1,'b');

 call symputx('cm', cm, 'g');

run;

%put &cm.;

 

2. Use a SQL query to select. Note that I've changed table names from A to make it less confusing. 

 

proc sql;
create table want as
select * from table1
where month_dt in (select cm from A);
quit;

@new_sas_user_4 wrote:

Hi, 

 

I want the the current month date to refer to the prior months start date until 4th of the current month which I am able to do using the below code.

 

data A;

format CM date9.;

if day(today())>=5 then CM=intnx('month',today(),0,'b');

else CM=intnx('month',today(),-1,'b');

 

run;

 

 

Once I get CM I want to be able to use it in another program where I will use it in the where clause 

 

proc sql;

create table WANT as select * from A(a table in my library)

where month_dt= CM;

quit;

 

If i write it this way, I get the errror that CM does not exist in A 

 

Can this be done?

OR can CM be converted to a macro  so that it can be used in WANT ?

 

Thanks a lot in advance!!


 

new_sas_user_4
Obsidian | Level 7

Thanks a lot 🙂

 

Jagadishkatam
Amethyst | Level 16

From the below program dataset A is generated but i dont see any SET statement to use the existing dataset. so i dont if the dataset A is actually created. please check, if it is then the proc sql should work if the month_dt is also available in dataset A along with date CM which you derived. 

 

data A;
set xxxx;?
format CM date9.;
if day(today())>=5 then CM=intnx('month',today(),0,'b');
else CM=intnx('month',today(),-1,'b');
run;

 

 

Thanks,
Jag
Tom
Super User Tom
Super User

You are not reading any data into your dataset A. 

Are you just trying to make a dataset with only one observation based on the current date?

Do you need that dataset for anything?  If not then put the logic into your query.

proc sql;
create table WANT as
  select *
  from A
  where month_dt= 
case when (day(today())>=5) then intnx('month',today(),0,'b')
else intnx('month',today(),-1,'b') end
;
quit;

Or you can eliminate the CASE and just use boolean logic to generate the proper offset.

month_dt = intnx('month',today(),0-(day(today())<5),'b')

If your dataset is indexed on MONTH_DT then you might get better performance using macro code to make that date into a constant instead.

%let today=%sysfunc(today());
%let cm=%sysfunc(intnx(month,&today,%eval(0-(%sysfunc(day(&today)))<5),b));
....
where month_dt = &cm 
....
new_sas_user_4
Obsidian | Level 7

Thanks everyone!!

I am sorry for the confusion with the table names..

The first table I create named A is just for creating the date and I want to use this date to filter from another table (lets call it X).

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 5 replies
  • 1090 views
  • 2 likes
  • 4 in conversation