Macro quoting issue

Accepted Solution Solved
Reply
Super Contributor
Posts: 418
Accepted Solution

Macro quoting issue

Tom's suggestion lead me to my problem, and I found the issue. Please see my last comment to see the exact error I was causing!

As always, Thanks Tom

Hello everyone. Can anyone help me understand what is going wrong with the following code? I don't know what I did incorrectly. note that the code inside the macro works (if instead of "&dsin"  I simply replace it with "&inputfile" it works.

It seems that taking inputfile and making it an entry into the macro call is causing my macro to no longer work, which does not make any sense to me.

  %let inputfile=i:\projects\mycsv.csv;

%macro DatasetNames(dsin,dsout);

data &dsout;

infile "&dsin." dsd
lrecl=32000 truncover obs=1
;

length varnumCSV 8 name $2000.;

  input @;

  do varnumCSV = 1 to countw(_infile_,',','Q');

    input name @;

    output;

  end;

run;

%mend;

%DatasetNames(&inputfile,outputdataset);

In addition, the following code also works, it is a modifiation of the above.

  %let inputfile=i:\projects\mycsv.csv;

%macro DatasetNames(dsin,dsout);

data &dsout;

infile dsin. dsd
lrecl=32000 truncover obs=1
;

length varnumCSV 8 name $2000.;

  input @;

  do varnumCSV = 1 to countw(_infile_,',','Q');

    input name @;

    output;

  end;

run;

%mend;

%DatasetNames("&inputfile",outputdataset);

So adding the quotes into the macro call instead of within the macro itself causes this to run. I don't understand why however.

Thanks!


Accepted Solutions
Solution
‎08-29-2013 06:20 PM
Super User
Super User
Posts: 7,078

Re: Macro quoting issue

Posted in reply to Anotherdream

In the broken one you left out the & in the reference to &DSNIN.

Personally I prefer the second one as it adds the flexibility of allowing you to pass in a fileref rather than only accepting a physical path.

filename in 'i:\projects\mycsv.csv' ;

%DatasetNames(in,outputdataset);

View solution in original post


All Replies
Super Contributor
Posts: 418

Re: Macro quoting issue

Posted in reply to Anotherdream

Oh also, the error  I am getting is the following.

FAILed to open "I:\projects\mycsv.csv , Outputdataset);"  :  The system cannot find the file specified.

So it looks like the system is not recognizing that the first macro varaible is SEPERATED from the second one by the comma in the macro call. It's almost like it's blurring both of them together, which makes no sene to me.  :smileyangry:

Solution
‎08-29-2013 06:20 PM
Super User
Super User
Posts: 7,078

Re: Macro quoting issue

Posted in reply to Anotherdream

In the broken one you left out the & in the reference to &DSNIN.

Personally I prefer the second one as it adds the flexibility of allowing you to pass in a fileref rather than only accepting a physical path.

filename in 'i:\projects\mycsv.csv' ;

%DatasetNames(in,outputdataset);

Super Contributor
Posts: 418

Re: Macro quoting issue

Posted in reply to Anotherdream

Sorry that was a copy error. Even with the & inside thhis section of code it does not work and throws the same error.

So even with

  %let inputfile=i:\projects\mycsv.csv;

%macro DatasetNames(dsin,dsout);

data &dsout;

infile "&dsin." dsd
lrecl=32000 truncover obs=1
;

length varnumCSV 8 name $2000.;

  input @;

  do varnumCSV = 1 to countw(_infile_,',','Q');

    input name @;

    output;

  end;

run;

%mend;

%DatasetNames(&inputfile,outputdataset);

I am still getting errors

Super User
Super User
Posts: 7,078

Re: Macro quoting issue

Posted in reply to Anotherdream

Works fine for me. You might have unbalanced quotes somewhere. Kill SAS and start over.

MPRINT(DATASETNAMES):   data outputdataset;

MPRINT(DATASETNAMES):   infile "i:\projects\mycsv.csv" dsd lrecl=32000 truncover obs=1;

MPRINT(DATASETNAMES):   length varnumCSV 8 name $2000.;

MPRINT(DATASETNAMES):   input @;

MPRINT(DATASETNAMES):   do varnumCSV = 1 to countw(_infile_,',','Q');

MPRINT(DATASETNAMES):   input name @;

MPRINT(DATASETNAMES):   output;

MPRINT(DATASETNAMES):   end;

MPRINT(DATASETNAMES):   run;

Super Contributor
Posts: 418

Re: Macro quoting issue

Posted in reply to Anotherdream

I found the issue.  I'm an idiot lol.

This code is a small section of a large loop process, and one of the files had ((( in its name.  So  the macro call looked like follows.

%DatasetNames(i:\projects\mycsv(((Version1.csv,outputdataset);  As such, without the quotes I believe SAS was trying to read in a new set of () conditions, and thus it was combining macro variable 1 and 2 into 1 variable and causing all of my problems.

When I re-run the code with a file that does not have these ((( values the process works. (Plus it works with Method two).

In case I run into more of these files I am going to re-code the macros to use version two instead of version 1, as it is safer. Plus I agree with your comment Tom that you can pass in File Refs which seem like a added flexibility that I never thought of.


Thanks for your help!

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 222 views
  • 0 likes
  • 2 in conversation