【问题标题】:Creating new variable and Recoding SAS创建新变量并重新编码 SAS
【发布时间】:2017-11-08 17:33:17
【问题描述】:

我想知道是否有一种方法可以创建新变量并根据一系列值对其进行重新编码。是否可以给出一个范围或者我必须指定每个值。

data recode ;
set old ;
if value =
 {(291.0-291.5) OR (295.2) OR (297.3- 297.7) OR (300.5) OR (305.8)} 
then care = 'new' ;
if value =
 {(500.1-501.5) OR (595.2)} 
then care = 'old';
ELSE care = 'other';
run ;

【问题讨论】:

标签: sas


【解决方案1】:

SAS 有 in 运算符,但这对您不起作用,因为它仅适用于整数或列表。它明确不适用于范围。

将起作用的最小更改是:

data recode ;
set old ;
if ((291.0 le value le 291.5)  OR (297.3 le value le 297.7) OR (value in (295.2,300.5,305.8)))
then care = 'new' ;
if 500.1 le value le 501.5 or value=595.2
then care = 'old';
ELSE care = 'other';
run ;

不过,这需要大量输入。

PROC FORMAT 是个好主意,正如 J_Lard 指出的那样。

proc format;
  value caref
  291.0-291.5,297.3-297.7,295.2,300.5,305.8 = 'new'
  500.1-501.5, 595.2 = 'old'
  other='other'
  ;
quit;
data _null_;
  input value;
  care=put(value,caref.);
  put value= care=;
  datalines;
200
291
291.3
291.7
297.0
297.5
295.2
300.5
300.7
500.0
500.1
500.5
595.2
595.5
;;;;
run;

这些范围与它配合得很好,您可以从数据文件中读取它们(请参阅PROC FORMAT 上的CNTLIN 选项)或 excel 或其他。

您还可以设置数组并根据各种数组值检查值。

还有其他解决方案,但格式可能最适合您的特定需求,特别是不太复杂。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多