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

Must I macro quote to quote a quote, or not quote, if quote then what quoting method is best, and why? What's the compile time effect vs. execution time effect? what is the delta character wrapper that's used to quote a quote and how stuff works? Then unquote for the SAS complier to execute.

 

Here is a supposedly simple issue

 

filename filelist pipe 'dir "\\corp\sites\RIB1001\RMT4PMC\datafiles\MONTH END REPORTS\Monthly Files\2020\202002MonthEnd" /b' ;

Would like to parameterize Year/Yearmonthend values using

/*Month end information-values*/
%let Year_month=%sysfunc(intnx(month,%sysfunc(today()),-1),yymmn6.);
%put &=Year_month;
%let year=%substr(&Year_month,1,4);
%put &=year;

Quote with BQUOTE works

 

filename filelist pipe %unquote(%bquote('dir "\\corp\sites\RIB1001\RMT4PMC\datafiles\MONTH END REPORTS\Monthly Files\&year\&year_month.MonthEnd" /b')) ;

and as does with %STR

filename filelist pipe %unquote(%str(%'dir "\\corp\sites\RIB1001\RMT4PMC\datafiles\MONTH END REPORTS\Monthly Files\&year\&year_month.MonthEnd" /b%')) ;

My attempt to thoroughly understand:

1. Read the documentation - my dumb brain is still not satisfied

2. Gonna venture on a book by @Astounding /author Michelle B  on Safarionline(I have paid subscription), and see if that's covered

3. Ask here

 

I would like the finer details please. Please allow me to take time to acknowledge the responses I get once my damn brain processes it. Thank you in advance and my warm regards

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Your example seems simpler than the theoretical issues you started the thread with.

 

If you want macro triggers evaluated inside of your quoted string you need to use double quotes on the OUTSIDE.   When you want to use the character used as the outside quote inside the quoted string just double it.

 

%let topnode=\\corp\sites\RIB1001\RMT4PMC\datafiles\MONTH END REPORTS\Monthly Files;
filename filelist pipe "dir ""&topnode\&year\&year_month.MonthEnd"" /b";

You CAN use the %BQUOTE() function.  But that will result in a value that is macro quoted, which might solve some problems or introduce new ones depending on how you want to use the value that %BQUOTE() generates.

 

You can also avoid a lot of these macro quoting issues by just using SAS code instead of macro code.  For example you could use the FILENAME() function in a data step instead of the FILENAME statement to define your fileref FILELIST.

View solution in original post

6 REPLIES 6
Astounding
PROC Star

Adding to your reading list:

 

https://stats.idre.ucla.edu/wp-content/uploads/2016/02/bt185.pdf

 

Anything you find written by Susan O'Connor is bound to be relevant.  She was a prime author of macro language.

Ksharp
Super User

@Tom   advice to use

filename filelist pipe %sysfunc(quote(dir "\\corp\sites\RIB1001\RMT4PMC\datafiles\MONTH END REPORTS\Monthly Files\&year\&year_month.MonthEnd" /b)) ;

 

Or single quote

 69         %let year=2010;
 70         %let year_month=201003;
 71         
 72         %let x=%sysfunc(quote(dir '\MONTH END REPORTS\Monthly Files\&year\&year_month.MonthEnd' /b)) ;
 73         
 74         %put &x ;
 "dir '\MONTH END REPORTS\Monthly Files\2010\201003MonthEnd' /b"
 75         

 

Tom
Super User Tom
Super User

Your example seems simpler than the theoretical issues you started the thread with.

 

If you want macro triggers evaluated inside of your quoted string you need to use double quotes on the OUTSIDE.   When you want to use the character used as the outside quote inside the quoted string just double it.

 

%let topnode=\\corp\sites\RIB1001\RMT4PMC\datafiles\MONTH END REPORTS\Monthly Files;
filename filelist pipe "dir ""&topnode\&year\&year_month.MonthEnd"" /b";

You CAN use the %BQUOTE() function.  But that will result in a value that is macro quoted, which might solve some problems or introduce new ones depending on how you want to use the value that %BQUOTE() generates.

 

You can also avoid a lot of these macro quoting issues by just using SAS code instead of macro code.  For example you could use the FILENAME() function in a data step instead of the FILENAME statement to define your fileref FILELIST.

novinosrin
Tourmaline | Level 20

Thank you all for chiming in. I am afraid I really didn't get enough time to log on to Safarionline and read as my team and I got swamped with ad-hoc tasks that rose from nowhere. I really intended to keep the thread open and clear some of the conceptual doubts on the macro quote and unquote. Nonetheless, I acknowledge it's unethical to not close thread for much too long.

 

Sir @Tom  Filename function is indeed slick and does the job for the example though I would like you to know I would take the liberty in coming back to the thread to get my understanding on macro quoting squeaky clean.

 

Sir @Astounding  I couldn't find a book authored by the macro champ person you mentioned on O'reily but I have yours. Is it only UCLA papers? 

Astounding
PROC Star

@novinosrin ,

 

I don't have a list of papers that Susan has written, or know where to find them all.  To the best of my knowledge she has not written a book.  But she has presented at a few SAS conferences and any paper she has presented is worth reading.

 

Happy hunting!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 6 replies
  • 569 views
  • 2 likes
  • 5 in conversation