BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
aggers
Calcite | Level 5

Hello,

 

I have a set of tables in two libraries. I need to create these tables in a work library.

So basically let's assume I have the following:

lib1.table_a

lib1.table_b  and so on

lib2.table_a

lib2.table_b ...

 

Desired Output:

Work.Lib1_table_a

Work.Lib1_table_b 

Work.Lib2_table_a

Work.Lib2_table_b 

 

what have i done so far is:

%let Lib = Lib1.;
%let table = table_a;

 

data &table;
set &Lib&table;

run;

 

What I need to do is:

  • Macro should simply read the data from ANY DATASET in source library and then output the TARGET table which is an exact copy of the SOURCE table, but its name was passed using %let statement (which I think I've done)
  • Macro that includes “do while” statement application. The program should produce output when %let statement is set to YES, otherwise it should output error message in the log.

I am not fluent in the loop subject so plain language would be highly appreciated. 

 

Thank you

 

Regards

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@aggers wrote:

Desired Output:

Work.Lib1_table_a

Work.Lib1_table_b 

Work.Lib2_table_a

Work.Lib2_table_b 

 


How about this, it's not exactly the same as above, but it ought to serve the same purpose, with a lot less work, no macros or loops. LIB1 gets copied into WORK, and LIB2 gets copied into WORK2.

 

options dlcreatedir;
libname work2 "%sysfunc(getoption(WORK))/%sysfunc(uuidgen())" access=temp;

proc copy in=lib1 out=work mtype=DATA;
run;
proc copy in=lib2 out=work2 mtype=DATA;
run;

 

 

--
Paige Miller

View solution in original post

10 REPLIES 10
PeterClemmensen
Tourmaline | Level 20

Do you want all data sets from the spcified libraries to be copied?

aggers
Calcite | Level 5

Yes, and I have dozens of tables. The exercise needs to include %let statement and "do while". 

PaigeMiller
Diamond | Level 26

@aggers wrote:

Hello,

 

I have a set of tables in two libraries. I need to create these tables in a work library.

So basically let's assume I have the following:

lib1.table_a

lib1.table_b  and so on

lib2.table_a

lib2.table_b ...

 

Desired Output:

Work.Lib1_table_a

Work.Lib1_table_b 

Work.Lib2_table_a

Work.Lib2_table_b 

 

what have i done so far is:

%let Lib = Lib1.;
%let table = table_a;

 

data &table;
set &Lib&table;

run;

 

What I need to do is:

  • Macro should simply read the data from ANY DATASET in source library and then output the TARGET table which is an exact copy of the SOURCE table, but its name was passed using %let statement (which I think I've done)
  • Macro that includes “do while” statement application. The program should produce output when %let statement is set to YES, otherwise it should output error message in the log.

I am not fluent in the loop subject so plain language would be highly appreciated. 


 

PROC COPY does this without macros, without loops and without DO WHILE.

 

proc copy in=lib1 out=work mtype=DATA;
run;
--
Paige Miller
aggers
Calcite | Level 5
Thank you for that, it works but I need to use %let statement and "do while".
aggers
Calcite | Level 5
it's a part of an exercise I have to do. I know there are many ways of completing this, but I need to use %let and "do while" statements to complete my task.
ballardw
Super User

@aggers wrote:
it's a part of an exercise I have to do. I know there are many ways of completing this, but I need to use %let and "do while" statements to complete my task.

Any WHILE or UNTIL loop requires 1) what to loop over, 2) an incrementing value rule and 3) an explicit stopping rule. Which values are you looping over? It is not clear exactly clear what the "variable" or "values" would be: 1 and 2 above.

 

You also have not supplied any idea of the stopping rule.

 

I will also say, besides other comments about use the correct tool for a task such as Proc Copy or Datasets in this case, that macro While and Until loops are often much harder to manage, at least in my experience then iterated loops over a known list of values.

PaigeMiller
Diamond | Level 26

@aggers wrote:
it's a part of an exercise I have to do. I know there are many ways of completing this, but I need to use %let and "do while" statements to complete my task.

So its a homework assignment? Usually, we expect you to write the code as far as you can go, and then if it's not working, show us the code and the LOG and we can help you get beyond that point.

--
Paige Miller
novinosrin
Tourmaline | Level 20
/*concatenate the two libnames to one*/
libname comb (lib1 lib2);
/*then copy the combined libname to work using proc datasets or proc copy suggested by Paigemiller*/
proc datasets library=comb;
   copy out=work;
run;
PaigeMiller
Diamond | Level 26

@aggers wrote:

Desired Output:

Work.Lib1_table_a

Work.Lib1_table_b 

Work.Lib2_table_a

Work.Lib2_table_b 

 


How about this, it's not exactly the same as above, but it ought to serve the same purpose, with a lot less work, no macros or loops. LIB1 gets copied into WORK, and LIB2 gets copied into WORK2.

 

options dlcreatedir;
libname work2 "%sysfunc(getoption(WORK))/%sysfunc(uuidgen())" access=temp;

proc copy in=lib1 out=work mtype=DATA;
run;
proc copy in=lib2 out=work2 mtype=DATA;
run;

 

 

--
Paige Miller

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 10 replies
  • 1356 views
  • 3 likes
  • 5 in conversation