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?
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.
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;
I was able to figure it out. I had to use dlm='09'x in the infile statement and then it worked...
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: