Help using Base SAS procedures

How to loop this proc SQL

Posts: 38

How to loop this proc SQL

I have the following Proc Sql to create sql tables, Instead of reapeating the commands I would like to know if I can use DO loop in proc SQL (all data in different years are available in the library)

proc sql;

     create table sqldb.ACxC_1993 as

     select * from CxC1993;

     create table sqldb.AValuAdded_1993 as

     select * from ValuAdded1993;

      create table sqldb.AFinalDemand_1993 as

     select * from FinalDemand1993;

     create table sqldb.ACxC_1994 as

     select * from CxC1994;

     create table sqldb.AValuAdded_1994 as

     select * from ValuAdded1994;

      create table sqldb.AFinalDemand_1994 as

     select * from FinalDemand1994;

     create table sqldb.ACxC_1995 as

     select * from CxC1995;

     create table sqldb.AValuAdded_1995 as

     select * from ValuAdded1995;

      create table sqldb.AFinalDemand_1995 as

     select * from FinalDemand1995;



Thank you

Posts: 8,740

Re: How to loop this proc SQL


  One possible solution would be to use a SAS Macro program to create your repetitive code. Since the only thing that changes is the year suffix, you could use a macro variable, such as &YR as a placeholder, which would be resolved into a year value when the macro program was invoked.

  This user group paper outlines the basics of using SAS Macro variables and SAS macro programs and will be helpful in getting you pointed in the right direction. In addition, there have been many, many previous forum postings on the use of Macro variables and Macro programs to generate code. (look on page 2 at the PROC MEANS example)

  What you have to remember is that the SAS Macro Facility only exists to generate code. So the first thing you have to do is start with a working SAS program, then figure out where in your program you can use macro variables or macro %DO loops to generate repetitive code.

  It looks like you have a good start with a working SAS program. Now you need to learn how to add macro variables and macro programs into the mix.


Super User
Super User
Posts: 6,498

Re: How to loop this proc SQL

It would be a lot easier if you weren't renaming the datasets.  Then you could just use PROC COPY.

proc copy in=work out=sqldb ;

select CxC1993 ValuAdded1993 FinalDemand1993 .... ;


Super User
Posts: 9,671

Re: How to loop this proc SQL

Or Call execute(). Why not try to use PROC COPY .it is a powerful tool to copy tables.

data _null_;
 set sashelp.vmember(keep=memname libname where=(libname='WORK')) end=last;
 if _n_ eq 1 then call execute('proc sql;');
 call execute('create table sqldb.A'||strip(memname)||' as select * from '||strip(memname)||' ;');
 if last then call execute('quit;');


Posts: 38

Re: How to loop this proc SQL

Thank you all, it was very helpful.

Ask a Question
Discussion stats
  • 4 replies
  • 4 in conversation