Reading in multiple datasets in a do-loop

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

Reading in multiple datasets in a do-loop

Hello:

I am trying to read in several SAS datasets for US states, each with a filename that has the format <statename>births i.e. ARnew, AZnew, CAnew,

I have 45 such datasets that I want to combine. I am using a do-loop for this but get an error saying "work.set.data. does not exist". What am I doing wrong?

libname proj '... ';

%let statelist=%str(ak,ar,al,.....wi,wv);

%macro combine;

data test;

%do k=1 %to 45;

%let state=%sysfunc(strip(%sysfunc(scan(&statelist,&k,','))));

set proj.&state.new

%end;

;

run;

%mend;

%combine;

Thanks

Dana


Accepted Solutions
Solution
‎08-12-2015 02:45 PM
Occasional Contributor
Posts: 10

Re: Reading in multiple datasets in a do-loop

I made the change but this didn't work.

libname proj '... ';

%let statelist=%str(ak,ar,al,.....wi,wv);

%macro combine;

data test;

%do k=1 %to 45;

set

%let state=%sysfunc(strip(%sysfunc(scan(&statelist,&k,','))));

proj.&state.new

%end;

;

run;

%mend;

%combine;

I still get the message

"work.set.data does not exist."

View solution in original post

Solution
‎08-12-2015 03:15 PM
Super User
Posts: 5,359

Re: Reading in multiple datasets in a do-loop

The word SET does not belong inside the %DO loop.  Move it to before the %DO statement, leaving everything else as is.

View solution in original post


All Replies
Super User
Posts: 11,114

Re: Reading in multiple datasets in a do-loop

One thing do check with unexpected macro results is to turn on the macro inspection options:

If you tried:

options mprint;

%combine;

You would likely find in the log:

Set proj.aknew set proj.arnew  and so forth.

YOu did not put your SET in the correct place so SAS thinks you are looking for a data set name SET.

Use:

set

%let state=%sysfunc(strip(%sysfunc(scan(&statelist,&k,','))));

proj.&state.new

%end;

;  /* now this ; properly closes the SET statement*/

Solution
‎08-12-2015 02:45 PM
Occasional Contributor
Posts: 10

Re: Reading in multiple datasets in a do-loop

I made the change but this didn't work.

libname proj '... ';

%let statelist=%str(ak,ar,al,.....wi,wv);

%macro combine;

data test;

%do k=1 %to 45;

set

%let state=%sysfunc(strip(%sysfunc(scan(&statelist,&k,','))));

proj.&state.new

%end;

;

run;

%mend;

%combine;

I still get the message

"work.set.data does not exist."

Solution
‎08-12-2015 03:15 PM
Super User
Posts: 5,359

Re: Reading in multiple datasets in a do-loop

The word SET does not belong inside the %DO loop.  Move it to before the %DO statement, leaving everything else as is.

Super User
Posts: 11,114

Re: Reading in multiple datasets in a do-loop

copy and paste lost the %do loop. sorry. Yes SET OUTSIDE the do loop per Astounding.

Occasional Contributor
Posts: 10

Re: Reading in multiple datasets in a do-loop

Thank You to ballardw & astounding!

🔒 This topic is solved and locked.

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

Discussion stats
  • 5 replies
  • 275 views
  • 0 likes
  • 3 in conversation