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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ScottBass
Rhodochrosite | Level 12
%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.


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.

View solution in original post

3 REPLIES 3
Reeza
Super User
Look at the modifiers in the SCAN function and how they can change the treatment of consecutive delimiters.
PaigeMiller
Diamond | Level 26
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;

 

--
Paige Miller
ScottBass
Rhodochrosite | Level 12
%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.


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.

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
  • 3 replies
  • 2291 views
  • 1 like
  • 4 in conversation