Delete all datasets observations in a specific library

Accepted Solution Solved
Reply
Super Contributor
Posts: 490
Accepted Solution

Delete all datasets observations in a specific library

How to delete every observation in each dataset in a specific lib.

PROC DELETE delete all the data set, what  i need is to delete all the data set observations not the data sets it self.


Accepted Solutions
Solution
‎10-16-2014 09:13 AM
Super User
Posts: 9,854

Re: Delete all datasets observations in a specific library

Sure .Easy.

data class;set sashelp.class;run;
data cars;set sashelp.cars;run;

data _null_;
 set sashelp.vmember(where=(libname='WORK' and memtype='DATA')) end=last;
 if _n_ eq 1 then call execute('proc sql;');
 call execute(cat('create table _',trim(memname),' like ',trim(memname),';'));
 if last then call execute('quit;');
run;

Xia Keshan

View solution in original post


All Replies
Respected Advisor
Posts: 3,782

Re: Delete all datasets observations in a specific library

data class1 class2;
   set sashelp.class;
   run;

*Keep the data set but delete the obs;
data class1;
   modify class1;
   remove;
  
run;
proc contents data=class1;
   *reports 0 obs and 19 deleted obs;
  
run;



*Create the data set new with 0 obs;
data class2;
   stop;
  
set class2;
   run;
proc contents data=class2;
   *reports 0 obs and 0 deleted obs;
  
run;

Super User
Posts: 9,854

Re: Delete all datasets observations in a specific library

Here are two ways :

data class;

set sashelp.class;

stop;

run;

proc sql;

create table class like sashelp.class ;

quit;

Xia Keshan

Super Contributor
Posts: 490

Re: Delete all datasets observations in a specific library

What if the data sets have different names not a series like class1 , class2 ......

and i do not know there names in advance_ the data sets i mean_, all what i know is the lib name

Super User
Posts: 7,371

Re: Delete all datasets observations in a specific library

You can get the table names from sashelp.vmember.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎10-16-2014 09:13 AM
Super User
Posts: 9,854

Re: Delete all datasets observations in a specific library

Sure .Easy.

data class;set sashelp.class;run;
data cars;set sashelp.cars;run;

data _null_;
 set sashelp.vmember(where=(libname='WORK' and memtype='DATA')) end=last;
 if _n_ eq 1 then call execute('proc sql;');
 call execute(cat('create table _',trim(memname),' like ',trim(memname),';'));
 if last then call execute('quit;');
run;

Xia Keshan

Regular Learner
Posts: 1

Re: Delete all datasets observations in a specific library

Sorry, but I can´t agree.

 

Both solutions create a new table with the structure of another one.

 

This code does delete records from a data set:

 

Data Lib.Table;

    Set Lib.Table;

    Stop;

run;

 

Similar but not the same;

 

Regards

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 1527 views
  • 8 likes
  • 5 in conversation