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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

5 REPLIES 5
Anotherdream
Quartz | Level 8

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:

Tom
Super User Tom
Super User

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);

Anotherdream
Quartz | Level 8

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

Tom
Super User Tom
Super User

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;

Anotherdream
Quartz | Level 8

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!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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