无所事事。
1. 判断空数据集
有的时候受试者还没入组,或者对侧compare的数据集还没生成,就需要先判断数据集是否为空。这有两种情况:
- 数据集存在,但观测数为0
- 数据集不存在
对于数据集存在的情况, 可以用sql获得观测数,并判断是否为0
proc sql noprint;
select count(*) into: nobs from work.test;
quit;
如果数据集不存在,多数情况下是不知道数据集是否存在, 需要先判断
%macro test;
%if %sysfunc(exsit(work.test)) %then %do;
%let dsid = %sysfunc(open(work.test));
%let nobs = %sysfunc(attrn(&dsid,nobs));
%let rc = %sysfunc(close(&dsid));
%end;
%else %let nobs = .;
%mend;
2. Time Functions
DATEPART(datetime): 从SAS datetime值中提取日期。
DHMS(date, hour, minute, second): 从日期、小时、分钟和秒的值返回SAS datetime值。
应用的前提是raw data是 sas datetime的格式,或者说是数值,而不是字符串。相应的函数返回结果也是date, time, datetime格式。
data vs;
set vital(in=a) weight(in=b);
if a then do;
if ^missing(vsdt) then adt = datepart(vsdt);
if nmiss(vsdt,vstm)=2 then adtm = dhms(adt,0,0,input(vstm,time.));
end;
if b then do;
if ^missing(wtdt) then adt = datepart(wtdt);
if ^missing(wtdt) then adtm = wtdt;
end;
format adt date9. adtm datetime20.;
run;
做SDTM/ADAM的时候涉及时间还有一个ISO8601的格式是必须知道的,完整格式为YYYY-MM-DDThh:mm:ss。 sas提供了一个很好用的format: E8601DT。
data time;
a = dhms('04MAR2024'd,0,0,0);
b = dhms('04MAR2024'd,0,0,98);
iso_a = put(a,e8601dt.);
iso_b = put(b,e8601dt.);
format a b datetime.;
run;
3. Functions: the existence of a file
a) 查看指定逻辑库下的成员是否存在
EXIST(member-name <, member-type <, generation>>): 判断当前已分配的SAS逻辑库中是否存在指定的SAS数据集。
member-name:指定的SAS member的名字
member-type:member的类型,最常用也是默认是DATA,也就是SAS数据集;还可以是ACCESS, CATALOG 和 VIEW
%let dsname=sashelp.cars;
%macro opends(name);
%if %sysfunc(exist(&name)) %then %do;
%let dsid=%sysfunc(open(&name, i));
%put dsid is &dsid.;
%end;
%else %put Data set &name does not exist.;
%mend opends;
%opends(&dsname);
这里会log会显示: dsid is 1
b) 查看指定路径下的外部文件是否存在
FILEEXIST(file-name):通过物理名称验证外部文件是否存在。
filename:指定操作环境下的完整的具体文件名(包括文件后缀)。
%let myfilerf=c:\test.txt;
%macro test;
%if %sysfunc(fileexist(&myfilerf)) %then
%let fid=%sysfunc(fopen(&myfilerf));
%else
%put The external file &myfilerf does not exist.;
%mend test;
options mprint;
%test
FEXIST(fileref):通过fileref验证相关联的外部文件是否存在。
fileref:通常先用 filename statement 指定全路径下文件。
filename mytest 'c:\test.txt';
%let fref=mytest;
%macro test;
%if %sysfunc(fexist(&fref)) %then
%put The file identified by the fileref &fref exists.;
%else
%put %sysfunc(sysmsg());
%mend test;
%test
4. Functions: the existence of a macro variable
a) %SYMEXIST(macro-variable-name): 验证指定的宏变量是否存在。
macro-variable-name:宏变量的名字 (不需要引号)
1
if the macro variable is found0
if the macro variable is not found
%if %symexist(var_name)=1 %then %let test_var_name = &var_name.;
b) SYMEXIST(argument): 验证指定的宏变量是否存在。
argument: 宏变量的名字(需要引号,但不需要&号)
if symexist("x") then put "x EXISTS";
5. Functions: the existence of a macro defination
%SYSMACEXIST(macro-name):判断是否存在指定的宏。
在WORK.SASMacr 目录下去搜索,如果有返回1,如果没有返回0。