BookmarkSubscribeRSS Feed
SKG
Obsidian | Level 7 SKG
Obsidian | Level 7

How to copy the all datasets structure from one library to another new library through sas base code

10 REPLIES 10
RW9
Diamond | Level 26 RW9
Diamond | Level 26

In what sense?  You just want to copy everything from one library to another:

proc copy in=inlib out=outlib;
run;

If you want to create empty versions then a bit more code, and it doesn't really make sense doing this at all:

data _null_;
  set sashelp.vcolumn (where=(libname="INLIB"));
  by memname;
  if first.memname then do;
    call execute('proc sql; create table OUTLIB.'||strip(memname))||' (');
    call execute(strip(name)||ifc(type="char","CHAR("||strip(put(length,best.))||")","NUM"));
  end;
  else call execute(","||strip(name)||ifc(type="char","CHAR("||strip(put(length,best.))||")","NUM"));
  if last.memname then call execute(');quit;');
run;

This will generate the code necessary to create each table.  Again, this is Not advised.

You could also sql describe table to a text file and then include that.

SKG
Obsidian | Level 7 SKG
Obsidian | Level 7

59 data _null_;
60 set sashelp.vcolumn (where=(libname="Maps"));
61 by memname;
62 if first.memname then do;
63 call execute('proc sql; create table work.'||strip(memname)||' (');
64 call
64 ! execute(strip(name)||ifc(type="char","CHAR("||strip(put(length,best.))||")","NUM"));
65 end;
66 else call
66 ! execute(","||strip(name)||ifc(type="char","CHAR("||strip(put(length,best.))||")","NUM"));
67 if last.memname then call execute(');quit;');
68 run;

NOTE: The map data sets in library MAPSGFK are based on the digital maps from GfK GeoMarketing
and are covered by their Copyright. For additional information, see
http://support.sas.com/mapsonline/gfklicense.
NOTE: There were 0 observations read from the data set SASHELP.VCOLUMN.
WHERE libname='Maps';
NOTE: DATA statement used (Total process time):
real time 12.31 seconds
cpu time 1.10 seconds

 

 

I'm not getting any observations from this code.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

And did you look at the view vcolumn in the library sashelp?  If you did you will see that libname variable is all upper case:

set sashelp.vcolumn (where=(libname="MAPS"));

 

 

Ksharp
Super User

Make a macro to go through one of the both following code.

 

1)  

data x.have;

 set y.have;

 stop;

run;

 

2)

proc sql;

create table x.have like y.have;

quit;

Ksharp
Super User
libname x v9 'c:\temp';

data _null_;
 set sashelp.vmember(keep=libname memname where=(libname='X'));
 call execute(catt('data work.',memname,';set x.',memname,';stop;run;'));
run;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Yes, good point @Ksharp, avoids need to do each column processing.  

SKG
Obsidian | Level 7 SKG
Obsidian | Level 7

69 data _null_;
70 set sashelp.vmember(keep=libname memname where=(libname='Maps'));
71 call execute(catt('data work.',memname,';set x.',memname,';stop;run;'));
72 run;

NOTE: There were 0 observations read from the data set SASHELP.VMEMBER.
WHERE libname='Maps';
NOTE: DATA statement used (Total process time):
real time 0.12 seconds
cpu time 0.12 seconds

 

 

I'm not getting any observations from this code.

Ksharp
Super User

Library name should be upcase.

 

libname='MAPS'

 

also change

;set x.',memname

into

;set maps.',memname

LinusH
Tourmaline | Level 20
To copy the full structure, create a macro that copies the tables usin PROC APPEND with obs=0 dataset option on the DATA= data set.
This way you will also be able to replicate structural components such as indexes and constraints.
Data never sleeps
Tom
Super User Tom
Super User

Use PROC COPY to copy the datasets. Use option OBS=0 to prevent it from copying the data.

options obs=0;
proc copy inlib=IN outlib=OUT;
run;
options obs=max;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 4615 views
  • 6 likes
  • 5 in conversation