Hello,
I need to copy the dataset from one library to another but it is based on the prefix of the dataset name.
For example; I have library “Libref_all” and I have below datasets in this library,
Libref_all |
A_DM1.sas7bdat |
A_DM2.sas7bdat |
A_DM3.sas7bdat |
A_DM4.sas7bdat |
B_DM1.sas7bdat |
B_DM2.sas7bdat |
B_DM3.sas7bdat |
B_DM4.sas7bdat |
C_DM1.sas7bdat |
C_DM2.sas7bdat |
D_DM1.sas7bdat |
I want to move these datasets based on their names, like dataset having prefix “A_” should copy to “Libref_A” library, similarly “B_” should copy to “Libref_b”, “C_” should copy to “Libref_C” and “D_” should copy to “Libref_D”.
Thanks in advance.
Try this. This builds a Proc Copy call for each obs. You can see the call in the data set.
Uncomment the Call Execute part and see if it works.
data have;
input Libref_all $20.;
datalines;
A_DM1.sas7bdat
A_DM2.sas7bdat
A_DM3.sas7bdat
A_DM4.sas7bdat
B_DM1.sas7bdat
B_DM2.sas7bdat
B_DM3.sas7bdat
B_DM4.sas7bdat
C_DM1.sas7bdat
C_DM2.sas7bdat
D_DM1.sas7bdat
;
data callstack;
set have;
ds = scan(Libref_all, 1);
lb = cats("Libref_", char(Libref_all, 1));
call = compbl(cat(
"proc copy in = Libref_all out = ", lb, "memtype=data;
select ", ds, ";
run;"
));
*call execute (call);
run;
The code will work without the Datalines/Cards Statement. They are purely for demonstration.
Try running the code against your actual data and check the callstack data. Does the call variable contain Proc Copy steps that look alright?
If so, try uncommenting the Call Execute Statement and see what happens.
If you have datasets listed like you presented, you could try something like this:
data _null_;
input Libref_all $ 20.;
call execute(
"data Libref_" !! scan(Libref_all,1,"_") !! '.' !! scan(Libref_all,1,".") !! "; "
!! "set Libref_all." !! scan(Libref_all,1,".") !! "; run;"
!! "proc delete data = Libref_all." !! scan(Libref_all,1,".") !! "; run;"
);
cards;
A_DM1.sas7bdat
A_DM2.sas7bdat
A_DM3.sas7bdat
A_DM4.sas7bdat
B_DM1.sas7bdat
B_DM2.sas7bdat
B_DM3.sas7bdat
B_DM4.sas7bdat
C_DM1.sas7bdat
C_DM2.sas7bdat
D_DM1.sas7bdat
;
run;
the `call execute()` will produce the code for you. I'm assuming you don't have any indexes and datasets aren't compressed.
Bart
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.