Help using Base SAS procedures

How can I mask the colon in the path, while invoking a macro?

Reply
Contributor
Posts: 49

How can I mask the colon in the path, while invoking a macro?

%macro import(path,name, out, sheet);

proc import datafile ="&path\&name.xls"

out=&out(where= (refid ~= .))

dbms=excel replace;

sheet= "&sheet";

mixed = yes;

run;

%mend import;

Suppose I want call macro with path like "d:\Clin-Dem\HCC\New Folder". How can I mask the colon in the path?

%import(%superq(d:\Clin-Dem\HCC\New Folder),%str(Cheng, Ann-Lii_2009_vol10), dataset4, Template);

Respected Advisor
Posts: 3,799

Re: How can I mask the colon in the path, while invoking a macro?

Posted in reply to devarayalu

You are using SUPERQ incorrectly.

I don't think the colon needs to be quoted.  You need two dots after &NAME any you may need to %UNQUOTE all that.

Super User
Posts: 11,343

Re: How can I mask the colon in the path, while invoking a macro?

Posted in reply to data_null__

And perhaps a period after &path

Here's an example that I use with multiple macro variables in an output file name:

%let  outpath= D:\Data\WIC\Nutrition Surveillance Rpt\Trend\output\ ;

ods rtf file="&outpath.district_&district._&syear._&eyear..rtf"

<options follow>

Respected Advisor
Posts: 3,799

Re: How can I mask the colon in the path, while invoking a macro?

Don't need one after &PATH it is followed by back-splash :smileyblush:

Super Contributor
Posts: 387

Re: How can I mask the colon in the path, while invoking a macro?

Posted in reply to devarayalu

Like data _null_ said, you don't have to quote the colon.

I suggest the following changes.  (I'm sending this from home, so don't have SAS in front of me to test):

1)  Use keyword instead of positional parameters.  It makes the syntax clearer, and IIRC removes the significance of the comma in your name parameter.

2)  If your parameters could contain tokens significant to the macro processor, you would use %superq *inside* the macro, not in the parameter declaration.

3)  If a macro variable contains text immediately following that would make the macro variable reference ambiguous, use a dot to mark the end of the macro variable reference.  So, like data _null_ said, you need two dots after &name, one to mark the end of the macro variable reference, and one as the filename extension delimiter.

4)  Most punctuation and whitespace will mark the end of a macro variable reference, so a following dot is not required.  I see some programmers "overuse" the dot to end a macro variable reference, when it's not required.  It works, but I personally find this syntax harder to read and a bit "ugly".  But that's just me.

Try this and see how it works for you:

%macro import(

   path=

   ,name=

   ,out=

   ,sheet=

);

proc import

   datafile="%superq(path)\%superq(name).xls"

   out=&out (where=(refid is not missing))

   dbms=excel

   replace;

   sheet="&sheet";

   mixed=yes;

run;

%mend;

Alternatively, you could use:

datafile="&path\&name..xls"

as long has your path or name variables aren't too funky, with characters that would confuse the macro processor.  Usually %superq() is needed in macro statements such as %if %superq(mvar) ne %str() ..., but while %superq in the above syntax may be over-cautious, it won't hurt.

%import(

   path=d:\Clin-Dem\HCC\New Folder

   ,name=Cheng, Ann-Lii_2009_vol10

   ,out=dataset4

   ,sheet=Template

);

If the comma in the name parameter confuses the macro processor, try:

name=%str(Cheng, Ann-Lii_2009_vol10)

HTH,

Scott

Super User
Posts: 10,020

Re: How can I mask the colon in the path, while invoking a macro?

Posted in reply to devarayalu

Why not use %str() ?

Ask a Question
Discussion stats
  • 5 replies
  • 445 views
  • 0 likes
  • 5 in conversation