美文网首页
185:proc delete和proc datasets删除临

185:proc delete和proc datasets删除临

作者: SASPRO的打工人生活 | 来源:发表于2023-01-12 18:10 被阅读0次

大家在新写一个程序的时候,开头除了程序的header,紧接着差不多就是这样一段程序了:

proc datasets library = work memtype = data nolist kill ;   

run;    

这样做的目的是为了删除你上一个程序产生的临时数据集,要知道临时数据集都是储存在WORK库,不会说你新建一个程序,那么这个程序产生的临时数据集都储存在对应的库了,那样相当于一个程序就有一个库,这不可能的。

大多数人可能只是看项目都这样写,然后就复制粘贴就是了,我也一样,今天我们就详细地学习一下proc delete和proc datasets;主要还是讲proc datasets,因为proc delete实现的功能比较少,且存在一些缺陷。

为什么要删除临时库?尤其是你写一个宏的时候,会产生很多的临时数据集,比如写table的时候,A组产生了6个临时数据集,B组只产生了4个临时数据集,一般输出数据集的时候,你可能会用6和4创建10个临时数据集。

然后你在后面的时候,为了省事,直接set数据集加个冒号,然后在处理B组的时候把A组多出的2个数据集(其余的4个数据集可能被覆盖了,可能倒不会出现什么问题)也算进去了,所以我们需要运行完一个宏之后把宏产生的临时数据集删除,再进行下一个宏的运行。

data final;

    set final_:;

run;

一:proc delete

先新建4个临时数据集

此时work库里面就产生了4个临时数据集

接着我们删除数据集:

①:删除单个数据集:

proc delete data=test1;

    run;

②:删除多个数据集

proc delete data=test1 test2 test3;

    run;

删除多个数据集的时候,还可以写成

proc delete data=test1-test3;

run;

但是遗憾的是proc delete不能这样批量删除数据集,这是缺陷之一:

proc delete data=test;

run;

当然,我们还能删除指定逻辑库里的数据,我们新建一个test库

proc delete lib=test data=test1;

run;

proc delete就讲这些,应该来说,proc delete能实现的功能,在proc datasets都能实现。

二:proc datasets

 Section1:proc datasets能实现各种各样的功能,删除数据集只是其中的功能之一,这个过程步还能实现重命名、复制、甚至修复数据集的功能。

The PROC DATASETS statement identifies the SAS data library containing the SAS files you want to modify. It is followed by one or more “RUN groups”, and a “QUIT” statement that ends the execution of the procedure.

也就是说proc datasets跟proc sql一样,quit才是真正结束过程步的结束语。在quit之前,你可以写多个run,每一个run都是单独的执行过程。比如下面这样:

上面的程序执行了两个过程,一个是modify;一个是append;产生了如下结果:

Section2:在proc datasets中能对数据集进行rename,format,赋label等操作,我觉得就像新写一个data步:

 ATTRIB – This statement allows you to specify the format, informat, or label statements for one or more variables.

 FORMAT – This statement lets you to assign formats to variables.

 INFORMAT – This statement permits to you assign informats to variables.

 LABEL – This statement allows you to create variable labels.

 RENAME – This statement lets you rename variables.

Section3:

因为proc datasets涉及到很多内容,但是很多功能我们实际工作中又不常用,所以最后再讲一个用proc datasets批量删除数据集的操作吧。

批量删除数据集名字开头一样的,可以用这样的操作,这也是proc datasets相比proc delete的优点:

写法就是上面这个,非常好用,放在宏里面就能避免数据集混乱。

至于这个gennum:restricts processing for generation data sets. You can use the option either in parentheses after the name of each SAS file or after a forward slash. Here is a list of valid values:

You can have SAS automatically keep historic versions of a SAS data set by creating a generation group for it. You specify the number of versions of the data set that SAS is to save when you create the generation group. Once a generation group is created, the old copy is saved as a member of the generation group every time you replace the data set. Its name is affixed with a pound sign and a 3-digit number indicating which generation data set it is. For example, if you specified a generation group of four for the SHOES data set and updated it three times, the current data set would be named SHOES, the next most current would be SHOES#003, the next most current would be SHOES#002, and the oldest version would be SHOES#001. Generation data sets within a generation group are automatically stored in the same SAS directory. Generation groups are a good way to keep track of past versions of important SAS data sets.

这上面的shoes可以换成你自己生成的数据集。但是我觉得用gennum=all就可以了。

相关文章

网友评论

      本文标题:185:proc delete和proc datasets删除临

      本文链接:https://www.haomeiwen.com/subject/daebcdtx.html