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

Hi,

I am fairly new to SAS.  This is the piece of sas code that I am running.  I am trying to get the same date  previous month based on  the value fetched from the database.

========================================

%macro load_table_dly;
proc sql noprint;
%put >>>>> CONNECTING TO DB <<<<<;
connect to &prod_mba_driver(&prod_mba_db_connection_passthru user=&userid password=&passwd);
%put >>>>> EXTRACTING BUSINESS DATE FROM THE DB <<<<<;
select bus_dt
into :v_bus_dt
from connection to &prod_mba_driver
(select bus_dt
from (select ''''||coalesce(max(mech.bus_dt),cast(&db2_day0. as date))||'''' as bus_dt
from yyyyyyy as mech
where 1=1
and cast(mech.cad_run_dtm as date) = &db2_day0.
) as mx_dt
);
%put >>>>> DISCONNECTING FROM PROD_MBA <<<<<;
disconnect from &prod_mba_driver;
quit;

%put &=v_bus_dt;   /*display the fetched date */
data dates;
datelastmonth = intnx('month',v_bus_dt,-1,'S');  /* Get the previous month, same date */
run;

%mend load_table_dly;
%load_table_dly;

==================================================

From the log file:

V_BUS_DT='2023-06-12'

NOTE: Variable v_bus_dt is uninitialized.   

 

Help appreciated

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Replace the following:

data dates;
datelastmonth = intnx('month',v_bus_dt,-1,'S');  /* Get the previous month, same date */
run;

 With:

data dates;
datelastmonth = intnx('month', input(&v_bus_dt, yymmdd10.),-1,'S');  /* Get the previous month, same date */
format datelastmonth yymmddd10.;
run;

@Sherry40 wrote:

Hi,

I am fairly new to SAS.  This is the piece of sas code that I am running.  I am trying to get the same date  previous month based on  the value fetched from the database.

========================================

%macro load_table_dly;
proc sql noprint;
%put >>>>> CONNECTING TO DB <<<<<;
connect to &prod_mba_driver(&prod_mba_db_connection_passthru user=&userid password=&passwd);
%put >>>>> EXTRACTING BUSINESS DATE FROM THE DB <<<<<;
select bus_dt
into :v_bus_dt
from connection to &prod_mba_driver
(select bus_dt
from (select ''''||coalesce(max(mech.bus_dt),cast(&db2_day0. as date))||'''' as bus_dt
from yyyyyyy as mech
where 1=1
and cast(mech.cad_run_dtm as date) = &db2_day0.
) as mx_dt
);
%put >>>>> DISCONNECTING FROM PROD_MBA <<<<<;
disconnect from &prod_mba_driver;
quit;

%put &=v_bus_dt;   /*display the fetched date */
data dates;
datelastmonth = intnx('month',v_bus_dt,-1,'S');  /* Get the previous month, same date */
run;

%mend load_table_dly;
%load_table_dly;

==================================================

From the log file:

V_BUS_DT='2023-06-12'

NOTE: Variable v_bus_dt is uninitialized.   

 

Help appreciated


 

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

In this data step you never give the variable a value.

data dates;
  datelastmonth = intnx('month',v_bus_dt,-1,'S');  
run;

Where is the value of V_BUS_DT supposed to come from?  Is it some other dataset? 

If so you forgot the SET statement.

 

Are you trying to use a macro variable named V_BUS_DT?  

If so you need to use & before the macro variable's name to have the macro processor insert its value into your code.

 

But it looks like you are putting a STRING into the macro variable.  So that will not work with the INTNX() function as it wants a DATE value there.  Dates are NUMBERS and not STRINGS.

Reeza
Super User

Replace the following:

data dates;
datelastmonth = intnx('month',v_bus_dt,-1,'S');  /* Get the previous month, same date */
run;

 With:

data dates;
datelastmonth = intnx('month', input(&v_bus_dt, yymmdd10.),-1,'S');  /* Get the previous month, same date */
format datelastmonth yymmddd10.;
run;

@Sherry40 wrote:

Hi,

I am fairly new to SAS.  This is the piece of sas code that I am running.  I am trying to get the same date  previous month based on  the value fetched from the database.

========================================

%macro load_table_dly;
proc sql noprint;
%put >>>>> CONNECTING TO DB <<<<<;
connect to &prod_mba_driver(&prod_mba_db_connection_passthru user=&userid password=&passwd);
%put >>>>> EXTRACTING BUSINESS DATE FROM THE DB <<<<<;
select bus_dt
into :v_bus_dt
from connection to &prod_mba_driver
(select bus_dt
from (select ''''||coalesce(max(mech.bus_dt),cast(&db2_day0. as date))||'''' as bus_dt
from yyyyyyy as mech
where 1=1
and cast(mech.cad_run_dtm as date) = &db2_day0.
) as mx_dt
);
%put >>>>> DISCONNECTING FROM PROD_MBA <<<<<;
disconnect from &prod_mba_driver;
quit;

%put &=v_bus_dt;   /*display the fetched date */
data dates;
datelastmonth = intnx('month',v_bus_dt,-1,'S');  /* Get the previous month, same date */
run;

%mend load_table_dly;
%load_table_dly;

==================================================

From the log file:

V_BUS_DT='2023-06-12'

NOTE: Variable v_bus_dt is uninitialized.   

 

Help appreciated


 

ballardw
Super User

Since this is occurring in macro code will you please

1) place this line before where you run the macro :

options mprint;

2) Then run the macro and share the entire log and generated code. Copy from the log, on the forum open a text box by clicking on the </> icon above the message window and then paste the text into the text box.

 

The option MPRINT will show details of the statements created by the macro processor, so we see much more detail of what actually occurs.

 

One issue: Something like '2023-06-12' is not a valid value to use with any of the date functions. That is just a character value. Dates are numeric and represent the number of days since 01JAN1960. If you try to create a literal date value it appears as 'ddMONyy'd  The dd are the days of the month, MON is the three-letter abbreviation of the Month and yy is the year, best as 4 digits to avoid any possible confusion. The whole value is wrapped inside quotes and is immediately followed by a letter D to tell SAS you intend this to be a date value.

 

You get uninitialized because in this statement:

datelastmonth = intnx('month',v_bus_dt,-1,'S');  /* Get the previous month, same date */

V_bus_dt is written as data step variable. Which you have not supplied as a variable in the data set so has no values assigned.

with the shown macro variable you can get a date value to use with INTNX as

datelastmonth = intnx('month',input(&v_bus_dt,yymmdd10.),-1,'S');  /* Get the previous month, same date */
Sherry40
Calcite | Level 5

Thank you all for the reply.  the 'input' command helped.  

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 450 views
  • 0 likes
  • 4 in conversation