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!
This did it, thanks!
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*/;
Very helpful, thanks!
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.