Help using Base SAS procedures

Help with macro function

Reply
Frequent Contributor
Posts: 127

Help with macro function

I am trying to read the text file on UNIX using infile. Below is the part of the code:   %if &&file&j.. =  %str(2015.02.04 Complete XYZ Data.txt)  %then  %do;           data  rawdata;   infile fromunix(%sysfunc(cats(&&file&j..)))  dlm="|"  dsd truncover lrecl=32707 firstobs=2;   %end; But somehow macro does not work mainly because there are periods and spaces for the file name.      If there is no period or space then it works. Is there a macro function I can use to make the above thing work with spaces and periods in the name of the file?

Super User
Super User
Posts: 7,961

Re: Help with macro function

Not sure why all the &&'s are needed, you could do something like:

filename my_dir pipe 'dir "c:\temp\*.txt" /b';  /* Note change to unix - this is Win */

data _null_;

  length buffer $2000.;

  infile my_dir;

  if my_dir="2015.02.04 Complete XYZ Data.txt" then do;

    call execute('data '||compress(buffer,' .1234567890')||';

                    infile "'||sstrip(buffer)||'";

                    input...;

                  run;');

  end;

run;

Or just use wildcards in the infile if you are ok with it all in one dataset.  I tend to find when there's lots of & and %'s all over things are bound to go wrong.

Frequent Contributor
Posts: 127

Re: Help with macro function

Hi RW9, Thanks for the solution.  I tried it but it does not seem to work. Below is my code without macro which runs on UNIX.   The zipped file has 2015.02.04 Complete XYZ Data.txt file in it.  When I run the below code I get the filenm variable with value = 2015.02.04 and not the full file name.     I tried your code but somehow it gives me error.  Can you please let me know how do I make it work on UNIX? Thanks a lot

filename xx pipe "zipinfo -1   /PROD/in/2015.02.04_Complete_XYZ_Data.zip";

data check;  

infile xx ;

length filenm $45; 

input filenm $;

run;

Frequent Contributor
Posts: 127

Re: Help with macro function

I was able to figure it out.  I had to use dlm='09'x in the infile statement  and then it worked...

Super User
Posts: 11,343

Re: Help with macro function

You can get a better appearing post by copying your text into a simple text editor like Notepad before posting here. (Also find the suggestion for the form to get an editor that accepts enhanced program editor code better and vote for it)

Is there any particular reason you are using %sysfunct and CATS?

When it comes to working with macros and files I tend to make file references to read data so the INFILE lines are simpler and the filename statement will tell me if the file was successfully assigned.

From what I see of your code you might actually be wanting

Filename fromunix "&&file&j";

and then the Infile would be

infile fromunix  dlm="|"  dsd truncover lrecl=32707 firstobs=2;

If you were getting errors you should run the code with OPTIONS MPRINT SYMBOLGEN and include that with the errors from the log.

Ask a Question
Discussion stats
  • 4 replies
  • 331 views
  • 0 likes
  • 3 in conversation