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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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