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

Hi there,

 

I have this macro that I am running and need to parse out "3 Month" and "6 Month" from the %mo_str_list line below. When using the scan function, however, it only pulls the "3" because of the spaces in "3 Month". I would like to have 3 Month and 6 Month separately to be able to create my report in the correct folder, as these are imputed into the file path when creating the report. Is there a way to do this? 

 

Below is the code I am using. The error I get just says that the file path does not exist.

 

%macro cityinc(city, zipcity);


%let months = 3mo 6mo /*9mo*/; /* Add or remove months as needed */
%let mo_dt_list = mo3_dt mo6_dt /*mo9_dt*/; /* Corresponding date variables */
%let mo_str_list = 3 Month 6 Month /*9 Month*/; /* Corresponding month descriptions */

ods listing close;

%do i = 1 %to %sysfunc(countw(&months));
%let month = %scan(&months, &i);
%let mo_dt = %scan(&mo_dt_list, &i);
%let mo_str = %scan(&mo_str_list, &i, q);

 

****REPORT CODE HERE*****

 

%end;

 

Thanks for your help!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
quickbluefish
Barite | Level 11
The easiest way would be to just add a delimiter (other than a space) to your mo_str_list macro variable, and then specify that delimiter as the 3rd argument in the corresponding SCAN function call. I usually use a pipe (|), but others will work too.
%let mo_str_list=3 Month|6 Month;
%let m=%scan(&mo_str_list,&i,|);

View solution in original post

4 REPLIES 4
quickbluefish
Barite | Level 11
The easiest way would be to just add a delimiter (other than a space) to your mo_str_list macro variable, and then specify that delimiter as the 3rd argument in the corresponding SCAN function call. I usually use a pipe (|), but others will work too.
%let mo_str_list=3 Month|6 Month;
%let m=%scan(&mo_str_list,&i,|);
Tom
Super User Tom
Super User

To tell %SCAN() (and COUNTW() also) to use space as the delimiter you need to TELL them you want to do that instead of letting them use their default set of delimiters that also includes other things like periods and commas.

%do i = 1 %to %sysfunc(countw(&months,%str( )));
%let month = %scan(&months, &i,%str( ));

But if you want to have values that include the delimiter like your second list you need to either use a different delimiter:

%let mo_str_list = 3 Month|6 Month/*|9 Month*/; 

And tell %SCAN() to use that delimiter instead.

%let mo_str = %scan(&mo_str_list, &i,|);

Or add quotes.

%let mo_str_list = '3 Month' "6 Month" /*"9 Month"*/; 

And tell %SCAN() to ignore delimiters inside of quotes. 

let mo_str = %scan(&mo_str_list, &i,%str( ),q);

You might want to also remove the quotes.

%let mo_str = %sysfunc(dequote(%scan(&mo_str_list, &i,%str( ),q)));

 

PS Put comments BEFORE the code they are commenting on to avoid that extra moment of confusion when reading the code where the programmer says "what is this statement for?"

/* Corresponding month descriptions */
%let mo_str_list = 3 Month|6 Month/*|9 Month*/; 

 

 

Corinthian94
Obsidian | Level 7

Very helpful, thanks!

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 826 views
  • 0 likes
  • 3 in conversation