BookmarkSubscribeRSS Feed
pp2014
Fluorite | Level 6

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?

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

pp2014
Fluorite | Level 6

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;

pp2014
Fluorite | Level 6

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

ballardw
Super User

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.

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!

What is Bayesian Analysis?

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.

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
  • 4 replies
  • 891 views
  • 0 likes
  • 3 in conversation