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
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
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.
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
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 */
Thank you all for the reply. the 'input' command helped.
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!
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.
Ready to level-up your skills? Choose your own adventure.