02-05-2015 10:47 AM
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?
02-05-2015 10:56 AM
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 */
length buffer $2000.;
if my_dir="2015.02.04 Complete XYZ Data.txt" then do;
call execute('data '||compress(buffer,' .1234567890')||';
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.
02-05-2015 01:26 PM
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";
infile xx ;
length filenm $45;
input filenm $;
02-05-2015 11:06 AM
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.