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

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
proc copy in=library_all out=library_a ;
select a_: ;
run;
proc copy in=library_all out=library_b ;
select b_: ;
run;

And Make a macro to go through all the table .

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

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;
Soham0707
Obsidian | Level 7
Thanks PeterClemmensen.

I have more than 1000 datasets to copy based on the name. Is there any other way to do this without datalines or cards statement?
I have provided an example that describes the scenario.
PeterClemmensen
Tourmaline | Level 20

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. 

yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Ksharp
Super User
proc copy in=library_all out=library_a ;
select a_: ;
run;
proc copy in=library_all out=library_b ;
select b_: ;
run;

And Make a macro to go through all the table .
Soham0707
Obsidian | Level 7
Thank you so much Ksharp. it's working.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

How to Concatenate Values

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.

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
  • 6 replies
  • 1173 views
  • 4 likes
  • 4 in conversation