I am using a Date Prompt to get a user input month in SAS. I get the input in the following format:
I need to read some automatically generated files having different file name formats, so based on the user input I need all these variables to dynamically define the files names and read them inside a program.
Can someone please help me to obtain the above?
I tried and my code does not work ☹️
Sorry for the long post.
Thanks for anyone giving some help!
======================================================================================================================
My code:
data _null_;
/* UserSelectedMonth is obtained from the Date prompt as a macro variable, but I just stated here so that that the rest of the code can be tested*/
%let UserSelectedMonth = 01JUL2022;
/*Try to obtain the Selected_month_no as a two digit no using the z2. format*/
%let Selected_month_no = %sysfunc(month("&UserSelectedMonth."d), z2.);
/*Try to obtain the Selected_month_name in three letters using the monname3. format*/
%let Selected_month_name = %sysfunc(month("&UserSelectedMonth."d), monname3.);
/*Extract the last two digits (Selected_year) using substr from DDMMMYYYY*/
%let Selected_year = %sysfunc(substr("&UserSelectedMonth",8,2));
/*Obtain Month_year_19 by combining Selected_month_name and 19*/
%let Month_year_19 = %sysfunc(cat(&Selected_month_name,19));
/*Obtain Selected_month by combining Selected_month_name, "-" and Selected_year */
%let Selected_month =%sysfunc(cat(&Selected_month_name,-,&Selected_year));
/*Obtain previous month from */
Previous_month = intnx('MONTH', "&UserSelectedMonth"d, -1, 'BEGINNING');
format Previous_month date9.;
call symputx('Previous_month ', put(Previous_month ,date9.));
%put _user_;
run;
=======================================================================
Macro variables I am getting:
GLOBAL MONTH_NAME Jan
GLOBAL MONTH_NO 07
GLOBAL MONTH_YEAR_19 Jan19
GLOBAL SELECTEDMONTH 01Jul2022
GLOBAL SELECTEDMONTH_END 31Jul2022
GLOBAL SELECTEDMONTH_LABEL Previous month
GLOBAL SELECTEDMONTH_REL M-1M
GLOBAL SELECTED_MONTH Jan-2
GLOBAL SELECTED_MONTH_NAME Jan
GLOBAL SELECTED_MONTH_NO 07
GLOBAL SELECTED_YEAR 02
A couple of observations.
%let Selected_month_name = %sysfunc(month("&UserSelectedMonth."d), monname3.);
It does the following
Here are suggested replacements:
%let UserSelectedMonth=01jul2022;
%let userdatevalue=%sysevalf("&UserSelectedMonth"d);
%let Selected_month_no =%sysfunc(putn(&userdatevalue,month2.));
%let Selected_month_name =%sysfunc(putn(&userdatevalue,monname3.));
%let Selected_year =%sysfunc(putn(&Userdatevalue,year2.));
%let month_year_19 =&selected_month_name.19;
%let previous_month =%sysfunc(intnx(month,&userdatevalue,-1),monyy7.);
The primary points here are:
A couple of observations.
%let Selected_month_name = %sysfunc(month("&UserSelectedMonth."d), monname3.);
It does the following
Here are suggested replacements:
%let UserSelectedMonth=01jul2022;
%let userdatevalue=%sysevalf("&UserSelectedMonth"d);
%let Selected_month_no =%sysfunc(putn(&userdatevalue,month2.));
%let Selected_month_name =%sysfunc(putn(&userdatevalue,monname3.));
%let Selected_year =%sysfunc(putn(&Userdatevalue,year2.));
%let month_year_19 =&selected_month_name.19;
%let previous_month =%sysfunc(intnx(month,&userdatevalue,-1),monyy7.);
The primary points here are:
Thank you very much 🙏
Following line is giving the Selected_month_no as a single digit no (for July it is giving 7 instead of 07).
%let Selected_month_no =%sysfunc(putn(&userdatevalue,month2.));
Any thoughts on how to get it as a two digit value? I tried using z2. but I think I did it incorrectly.
Thanks again
@madara155 wrote:
Following line is giving the Selected_month_no as a single digit no (for July it is giving 7 instead of 07).
%let Selected_month_no =%sysfunc(putn(&userdatevalue,month2.));
Any thoughts on how to get it as a two digit value? I tried using z2. but I think I did it incorrectly.
Thanks again
Please show what you did when you "tried using Z2.".
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.