DATA Step, Macro, Functions and more

Using a do loop to load multiple data sets

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Using a do loop to load multiple data sets

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;

 


Accepted Solutions
Solution
‎03-30-2017 03:16 PM
PROC Star
Posts: 7,471

Re: Using a do loop to load multiple data sets

Posted in reply to collinelliot

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

View solution in original post


All Replies
PROC Star
Posts: 307

Re: Using a do loop to load multiple data sets

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...);

PROC Star
Posts: 307

Re: Using a do loop to load multiple data sets

Posted in reply to collinelliot

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?

Solution
‎03-30-2017 03:16 PM
PROC Star
Posts: 7,471

Re: Using a do loop to load multiple data sets

Posted in reply to collinelliot

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

Occasional Contributor
Posts: 6

Re: Using a do loop to load multiple data sets

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!

Occasional Contributor
Posts: 6

Re: Using a do loop to load multiple data sets

Posted in reply to collinelliot

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.

PROC Star
Posts: 307

Re: Using a do loop to load multiple data sets

You'll need the full path names for that to work:

 

filename SIPP_FILES ('somepath\l08puw1.dat' 'somepath\l08puw2.dat' 'somepath\l08puw3.dat');

Occasional Contributor
Posts: 6

Re: Using a do loop to load multiple data sets

Posted in reply to collinelliot

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.

 

PROC Star
Posts: 307

Re: Using a do loop to load multiple data sets

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?

 

 

Occasional Contributor
Posts: 6

Re: Using a do loop to load multiple data sets

Posted in reply to collinelliot

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!

PROC Star
Posts: 307

Re: Using a do loop to load multiple data sets

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.

Super User
Posts: 11,343

Re: Using a do loop to load multiple data sets


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.

Occasional Contributor
Posts: 6

Re: Using a do loop to load multiple data sets

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

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 12 replies
  • 181 views
  • 3 likes
  • 4 in conversation