I have a macro variable containing the file name. The file name is in this format:
ABC_DE___January_7__2019_2019_12_20
I need to extract the date January 7, 2019 from this macro variable and assign it to another macro variable using call symput.
This is my code so far:
%let filename = ABC_DE___January_7__2019_2019_12_20;
data _null_;
set a;
call symput('file_year', trim(left(%scan(&filename., 5, _))));
call symput('file_month', trim(left(%scan(&filename., 3, _))));
call symput('file_day', trim(left(%scan(&filename., 4, _))));
run;
If I call on the %put statement, I get the following results:
file_year = 2019
file_day = 7
file_month = (blank)
In the log, it says "Variable January is uninitialized"
I don't understand how the file_month could be blank considering that they are all delimited by underscores.
There are 3 underscores before the month; 1 underscore before the day, and 2 underscores before the year. If it's because of multiple underscores, then why was SAS able to successfully extract the file_year?
Is it because the month is in text form, not number form? I would need to convert the month from text to number as well. I tried adding in a input(file_month, month.) but to no avail.
%let filename = ABC_DE___January_7__2019_2019_12_20;
%let file_day = %sysfunc(scan(&filename,-1,_));
%let file_month = %sysfunc(scan(&filename,-2,_));
%let file_year = %sysfunc(scan(&filename,-3,_));
%put &=file_year;
%put &=file_month;
%put &=file_day;
%symdel file_year file_month file_day;
* or if you want to use a data step ;
data _null_;
filename = "&filename";
length file_day file_month file_year $4;
file_day = scan(filename,-1,'_');
file_month = scan(filename,-2,'_');
file_year = scan(filename,-3,'_');
call symputx('file_day',file_day);
call symputx('file_month',file_month);
call symputx('file_year',file_year);
run;
%put &=file_year;
%put &=file_month;
%put &=file_day;
Your mix of macro and data step statements in your data step is convoluted, I don't recommend it.
call symput('file_month', trim(left(%scan(&filename., 3, _))));
extracts January from your text string, and then there is no data step variable named January to operate on. (You are doing this in a data step, that's why it thinks January is the name of a data step variable inside the left() function. Please note that when a macro variable or macro function is encountered in SAS code, its value is substituted and the result must be legal valid working SAS code. So the line of code for file_month resolves to
call symput('file_month', trim(left(January)));
which is only legal valid working SAS code if there is a variable named January.
How about this:
data _null_;
call symputx('file_year', scan("&filename", 5,'_'));
call symputx('file_month', scan("&filename", 3,'_'));
call symputx('file_day', scan("&filename", 4,'_'));
run;
%let filename = ABC_DE___January_7__2019_2019_12_20;
%let file_day = %sysfunc(scan(&filename,-1,_));
%let file_month = %sysfunc(scan(&filename,-2,_));
%let file_year = %sysfunc(scan(&filename,-3,_));
%put &=file_year;
%put &=file_month;
%put &=file_day;
%symdel file_year file_month file_day;
* or if you want to use a data step ;
data _null_;
filename = "&filename";
length file_day file_month file_year $4;
file_day = scan(filename,-1,'_');
file_month = scan(filename,-2,'_');
file_year = scan(filename,-3,'_');
call symputx('file_day',file_day);
call symputx('file_month',file_month);
call symputx('file_year',file_year);
run;
%put &=file_year;
%put &=file_month;
%put &=file_day;
Your mix of macro and data step statements in your data step is convoluted, I don't recommend it.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.