07-16-2012 04:29 PM
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)
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;
07-16-2012 05:35 PM
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://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.
07-16-2012 06:06 PM
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 .... ;
07-16-2012 10:35 PM
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