Chen
Chen

Reputation: 383

SAS macro variable invalid option name

I created a macro variable &dirLSB, and I set up its path with the code below:

%let dirLSB = "/folders/myfolders/LSB/"; 

libname FLOWER &dirLSB; *Worked!;

data FLOWER.magnolia; 

infile "&dirLSB.Mag.dat";

input ScientificName $ 1-14 CommonName $ 16-32 MaximumHeight AgeBloom 

Type $ Color $; run; 

proc print data=FLOWER.magnolia; title "Magnolias"; run;

It looked like a simple reading -in the data set, however, I encountered the error such as below:

    1          OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 72         
 73         data FLOWER.magnolia;
 74         infile "&dirLSB.Mag.dat";
 NOTE: Line generated by the macro variable "DIRLSB".
 74          ""/folders/myfolders/LSB/"Mag.dat
               _
               23
 ERROR 23-2: Invalid option name /.

 74       !  ""/folders/myfolders/LSB/"Mag.dat
                _______
                23
 ERROR 23-2: Invalid option name FOLDERS.

 74       !  ""/folders/myfolders/LSB/"Mag.dat
                        _________
                        23
 ERROR 23-2: Invalid option name MYFOLDERS.

 74       !  ""/folders/myfolders/LSB/"Mag.dat
                                  ___
                                  23
 ERROR 23-2: Invalid option name LSB.

 75         input ScientificName $ 1-14 CommonName $ 16-32 MaximumHeight
 76               AgeBloom Type $ Color $;
 77         run;

 NOTE: The SAS System stopped processing this step because of errors.
 WARNING: The data set FLOWER.MAGNOLIA may be incomplete.  When this step was stopped there were 0 observations and 6 variables.
 WARNING: Data set FLOWER.MAGNOLIA was not replaced because this step was stopped.
 NOTE: DATA statement used (Total process time):
       real time           0.03 seconds
       cpu time            0.02 seconds


 78         
 79         proc print data=FLOWER.magnolia;
 80             title "Magnolias";
 81             run;

 NOTE: No observations in data set FLOWER.MAGNOLIA.
 NOTE: PROCEDURE PRINT used (Total process time):
       real time           0.01 seconds
       cpu time            0.00 seconds


 82         
 83         OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 96         

I set up "LSB" in the "folder shortcuts" first, and then I checked its property and realized its path as "/folders/myfolders/LSB/" and then I changed the code. It was successful I created a library named "FLOWER", hence, I did not manage to understand what I had gone wrong.

Thank you very much!!!

Upvotes: 1

Views: 3446

Answers (1)

Richard
Richard

Reputation: 27498

Fix

Change the macro variable assignment to be with out double-quotes.

%let dirLSB = /folders/myfolders/LSB/; 

Thus the eventual infile statement will be correct and as-if you coded

infile "/folders/myfolders/LSB/.Mag.dat";

Explanation

A macro variable (also know as a macro symbol) has a value that is placed in the processing stream when the symbols resolution is requested with a preceding & (ampersand) character and followed by an optional . (period) character (a sentinel). Any non-name character will also act as a sentinel when SAS is determining the &symbol-name that is being requested to be resolved.

Macro variables are always 'strings', but not in a DATA step programmatic sense. Thus the assignment statement

%let dirLSB = "/folders/myfolders/LSB/"; 

is setting up a macro variable whose value is quite literally the following sequence of characters:

  • double quote
  • forward slash
  • letter f
  • etc …
  • capital B
  • forward slash
  • double quote

When you resolve the macro variable the double quotes go into the processing stream, thus the statement

infile "&dirLSB.Mag.dat";

will be processed as-if you coded

infile ""/folders/myfolders/LSB/"Mag.dat";

which you should recognized as a 'messed up' statement.

Upvotes: 2

Related Questions