首页 > 代码库 > [SAS]易错例子之数值型转字符型

[SAS]易错例子之数值型转字符型

/*
   数值型缺失,可赋值点号(.),data步可以用 var = . 或 missing(var)判断
   字符型缺失,可赋值0个或多个空格(‘‘ 或‘ ‘等), data步可以用var = ‘‘ 或missing(var)判断
   以下是数值型缺失转字符型的例子,但缺失转换后不缺失,易做错。
*/

data test;
input var;
datalines;
1
.
;
run;

/* 错误转换*/
data test2;
    set test;
    
	var2 = compress(put(var, 8.));
run;

proc sql;
    select count(*) as miss_n
	from test2
	where var2 is missing;
quit;

/* 正确转换*/
data test3;
    set test;
 
    if missing(var) then var2 = ‘‘;
    else var2 = compress(put(var, 8.));
run;

proc sql;
    select count(*) as miss_n
	from test3
	where var2 is missing;
quit;

  

[SAS]易错例子之数值型转字符型