Help using Base SAS procedures

Copy dataset within same library to new dataset

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 75
Accepted Solution

Copy dataset within same library to new dataset

Hi,
I'm trying to replicate a dataset (refreshed daily) in a library based on whether it's the first of the month or not.
I this I want to use a PROC because it's possible I might change the indexes on the dataset in the future.
data cls (index = (name) );
   set sashelp.class;
run;
proc datasets library = work;
   change cls = cls2; run;
quit;
proc contents data = work.cls2;
run;
data cls3 (index = (name) );
   set cls;
run;

^^
The first two datasteps kind of do what I'm after, but it removes the cls dataset, which I want to retain.

The third datastep was there for me to check that the index was still there after the change (and it was).

The final datastep is my current solution, but I don't really want to essentially recreate the dataset.

I'm running SAS 9.3 using Enterprise Guide 5.1 (5.100.0.12269) Hot fix 7 (32-bit)

Thanks.


Accepted Solutions
Solution
‎08-27-2013 03:24 AM
SAS Super FREQ
Posts: 708

Re: Copy dataset within same library to new dataset

Hi JohnT

You can not copy a SAS Data Set within the same library. However you can use other statements from Proc DATASETS to achieve the same. Have a look at the code below. It will delete an existing CLS2 data set. It then uses the APPEND statement, if the base table is not found, then append will make a copy of the data table and preserve index etc.

data cls (index = (name) label="Original CLS");
   set sashelp.class;
run;

proc datasets library = work;
  delete cls2;
run;
 
append  data=cls base=cls2;
run;
quit;

View solution in original post


All Replies
Super Contributor
Posts: 307

Re: Copy dataset within same library to new dataset

So . . . you want to keep the original dataset (CLS), and you don't need the final dataset (CLS3)?

Frequent Contributor
Posts: 75

Re: Copy dataset within same library to new dataset

Sorry I should have been more specific.  Ignore the last two steps, I want CLS and CLS2, they were just for checking and demonstrating my current solution.

data cls (index = (name) );
   set sashelp.class;
run;

proc datasets library = work;
   change cls = cls2; run;
quit;

Solution
‎08-27-2013 03:24 AM
SAS Super FREQ
Posts: 708

Re: Copy dataset within same library to new dataset

Hi JohnT

You can not copy a SAS Data Set within the same library. However you can use other statements from Proc DATASETS to achieve the same. Have a look at the code below. It will delete an existing CLS2 data set. It then uses the APPEND statement, if the base table is not found, then append will make a copy of the data table and preserve index etc.

data cls (index = (name) label="Original CLS");
   set sashelp.class;
run;

proc datasets library = work;
  delete cls2;
run;
 
append  data=cls base=cls2;
run;
quit;
Frequent Contributor
Posts: 75

Re: Copy dataset within same library to new dataset

Posted in reply to Bruno_SAS

Thanks Bruno, that solution works for me.

When I run it using my original solution compared to this one, the times are roughly the same, which is good.  And the fact that the indexes are maintained is exactly what I was after.

Thanks again.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 476 views
  • 0 likes
  • 3 in conversation