Hi all
I am trying to use a do loop to upload multiple data files that vary by a trailing integer (e.g. dataset1.dat, dataset2.dat, etc)
I tried using a macro, but have had issues. From the way the editor does not bold the data procedure it seems like it is not recognizing it as a command within the marco. Anyone have suggestions?
filename SIPP1 "C:\...SIPP\l08puw1.dat";
filename SIPP2 "C:\...SIPP\l08puw2.dat";
filename SIPP3 "C:\...SIPP\l08puw3.dat";
filename SIPP4 "C:\...SIPP\l08puw4.dat";
%macro uploadSIPP;
%local i;
%do i=1 %to 4;
data SIPPRAW&i;
INFILE SIPP&i PAD LRECL=2341;
INPUT
SSUSEQ 1- 5
SSUID $ 6- 17
SPANEL 18- 21
...
FILLER 2338-2340;
RUN;
%end;
%mend uploadSIPP;
%uploadSIPP;
Your code worked fine for me. You'd have to show us your log for anyone to know what your problem is.
Art, CEO, AnalystFinder.com
You can use one filename that refers to multiple files. If they are all the same structure, and in your example it looks like they are, this would be much easier.
filename all_files ('file1.txt' file2.txt' etc...);
That said, I wouldn't rely on the editor syntax highlighitng to tell you if it's correct code. Are you getting an error when you run what you? What are the issues?
Your code worked fine for me. You'd have to show us your log for anyone to know what your problem is.
Art, CEO, AnalystFinder.com
Weird. Now it works for me too. I guess I got caught up in the editor labeling. I also rebooted SAS so maybe that helped.
Thanks for the confirmation. I was driving myself crazy!
I had no idea that could be done. When I try however I am getting this error:
filename SIPP_FILES ('l08puw1.dat' 'l08puw2.dat' 'l08puw3.dat');
ERROR: Invalid logical name.
ERROR: Error in the FILENAME statement.
I have a feeling I am missing a basic point.
You'll need the full path names for that to work:
filename SIPP_FILES ('somepath\l08puw1.dat' 'somepath\l08puw2.dat' 'somepath\l08puw3.dat');
Even though it appears the old code works, I am intriqued by the naming of multiple files, but am still getting an error.
1047 filename SIPP_FILES ('C:\Users\...\Desktop\SIPP\l08puw1.dat'
1047! 'C:\Users\...\Desktop\SIPP\l08puw2.dat');
ERROR: Invalid logical name.
ERROR: Error in the FILENAME statement.
The error is saying that the path and or file is not valid.
Are you replacing the actual path with "..." or is that your actual syntax?
It appears SAS did not like the name SIPP_FILES. When I changed the name to SIPP_FIL the code worked. When I switched it back to SIPP_FILES it got angry again. Guess I should always remember to check the name.
Thank you for the tip by the way!
Note that you can use wildcards, too, so you could just replace the # with "*" and really limit your typing. Just make sure you're not pulling in something you don't want.
@hoops581 wrote:
It appears SAS did not like the name SIPP_FILES. When I changed the name to SIPP_FIL the code worked. When I switched it back to SIPP_FILES it got angry again. Guess I should always remember to check the name.
Thank you for the tip by the way!
FILENAME and LIBNAME are both limited to 8 characters in the reference name so since SIPP_FILES has 10 characters that is an error.
Good to know! I thought it was perhaps something related to having "FILE" in the name. That will save me many errors in the future. Thank you
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 25. Read more here about why you should contribute and what is in it for you!
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.