DATA Step, Macro, Functions and more

Running Procs Within a Do Loop

Occasional Contributor
Posts: 14

Running Procs Within a Do Loop

Hey all, question:

I have a dataset of users and what I'm looking to do is look through all of them and create a series of reports by user.  


table work.users






My report code is

/***************************Set Dynamic Path and Filename***********************************/

ODS tagsets.HTMLpanel nogtitle  Path="\\myreports\[username]"

File = "[username].xls" style=minimal


/*************Create Dynamic Table for each User*************/

proc sql;

create table [username] as

select sales_id, sales_date, price from sales where name = '[username]';


proc print data=[username];


ods html close;

In my other programming experience I'd use a do while loop but I'm stuck as to how to do it in SAS.  Any help would be greatly appreciated.

Super User
Posts: 13,542

Running Procs Within a Do Loop

If I understand what you want to do the easiest may be a BY group process.

Proc sort data=Sales out=SortedSales;by username;run;

proc print data=SortedSales;

by username;

var sales_id sales_date price;


Super User
Posts: 23,724

Re: Running Procs Within a Do Loop

Do you need to do it for all users or only a select users?

Have you looked at BY processing? Or Macro processing?

Basically theres a lot of ways to do it, I'd need more info to see what the best way to do it would be, but guessing you're going to go down the macro route if you want a file for each user.

See here for a basic idea:


Posts: 8,164

Re: Running Procs Within a Do Loop

Yet another way of doing it, but one which would accept the code basically how you have written it, is to submit the code within a datastep using call execute.  You can find an example of doing that in the following thread:

Since SAS inherently loops through the records during a datastep, you don't need to get overly loopy.

Super User
Posts: 10,778

Re: Running Procs Within a Do Loop

Just as Art said. You can firstly make a macro to include these SQL proc print, then use call execute to loop this macro.


Ask a Question
Discussion stats
  • 4 replies
  • 1 like
  • 5 in conversation