DATA Step, Macro, Functions and more

Do loop for where "i" equals specific values

Accepted Solution Solved
Reply
Contributor
Posts: 20
Accepted Solution

Do loop for where "i" equals specific values

Dataset FILES_TO_PROCESS looks like this:

FILE_NUM

401

412

439

I'd like to create a macro program with a do loop where "i" takes on those values. My workaround is something like this...

proc sql;

        select min(FILE_NUM) into :min_num

        from FILES_TO_PROCESS

        ;

        select max(FILE_NUM) into :max_num

        from FILES_TO_PROCESS

        ;

quit;

%macro start_processing;

    %do i = &min_num %to &max_num;

     <yadda yadda yadda>

%mend;

But of course that will cause the do loop to iterate 39 times, as opposed to just the 3 needed.

Help!


Accepted Solutions
Solution
‎11-02-2012 05:56 PM
Super User
Posts: 11,343

Re: Do loop for where "i" equals specific values

Here's a quick example of taking one field from multiple rows of data file and stepping through them. Look in the log for the output.

 

%macro dummy;

proc sql noprint;

select make into :cars separated by ' '

from sasuser.carpref

;

quit;

%do i = 1 %to &sqlobs ;

%let name= %scan(&cars,&i);

%put Do something with &name; /* this is where your useful code would go*/

%end;

%mend;

%dummy;

View solution in original post


All Replies
Super User
Posts: 19,865

Re: Do loop for where "i" equals specific values

Where is the files to process coming from, a SAS library or a windows or unix directory.

If they all have the same extension you can use a wildcard to process everything in one directory at once...depending on what you're doing of course.

Solution
‎11-02-2012 05:56 PM
Super User
Posts: 11,343

Re: Do loop for where "i" equals specific values

Here's a quick example of taking one field from multiple rows of data file and stepping through them. Look in the log for the output.

 

%macro dummy;

proc sql noprint;

select make into :cars separated by ' '

from sasuser.carpref

;

quit;

%do i = 1 %to &sqlobs ;

%let name= %scan(&cars,&i);

%put Do something with &name; /* this is where your useful code would go*/

%end;

%mend;

%dummy;

Contributor
Posts: 20

Re: Do loop for where "i" equals specific values

Thank you ballardw, that works like a charm! Apologies to the others for not providing the full details, but I guess I got lucky that ballardw was on the same wavelength Smiley Happy

PROC Star
Posts: 7,492

Re: Do loop for where "i" equals specific values

Glad to hear that you received an answer that you found useful.  However, in the future, the more details you can provide upfront, the more quickly and assuredly others can try to provide a useful response.

PROC Star
Posts: 7,492

Re: Do loop for where "i" equals specific values

I would suggest that you provide an example of what you really want to accomplish.

🔒 This topic is solved and locked.

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

Discussion stats
  • 5 replies
  • 389 views
  • 0 likes
  • 4 in conversation