BookmarkSubscribeRSS Feed
goliPSU
Calcite | Level 5

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;

quit;

run;

Thank you

4 REPLIES 4
Cynthia_sas
SAS Super FREQ

Hi:

  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.

http://www2.sas.com/proceedings/sugi28/056-28.pdf

http://www.nesug.org/proceedings/nesug07/cc/cc41.pdf (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.

cynthia

Tom
Super User Tom
Super User

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

run;

Ksharp
Super User

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;');
run

Ksharp

goliPSU
Calcite | Level 5

Thank you all, it was very helpful.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1997 views
  • 0 likes
  • 4 in conversation