SAS LOOP - 从具有值的记录中创建列 [英] SAS LOOP - create columns from the records which are having a value

查看:21
本文介绍了SAS LOOP - 从具有值的记录中创建列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有随机诊断代码,例如 001、v58、...、142、.. 如何从记录中为 1 的代码构造列?

Suppose i have random diagnostic codes, such as 001, v58, ..., 142,.. How can I construct columns from the codes which is 1 for the records?

Input:
            id found  code
            
            1   1      001
            2   0      v58
            3   1      v58
            4   1      003
            5   0      v58
    ......
    ......
    15000   0      v58

Output:
id code_001 code_v58 code_003 .......
1    1       0        0
2    0       0        0
3    0       1        0
4    1       0        0
5    0       0        0
.........
.........

推荐答案

您需要TRANSPOSE 值并根据数据(code 的值)命名透视列) 带有 ID 语句.

You will want to TRANSPOSE the values and name the pivoted columns according to data (value of code) with an ID statement.

例子:

在现实世界的数据中,缺失的诊断通常会被标记为零,这必须在后续步骤中完成.

In real world data it is often the case that missing diagnoses will be flagged zero, and that has to be done in a subsequent step.

data have;
input id found  code $;
datalines;            
1   1      001
2   0      v58
2   1      003  /* second diagnosis result for patient 2 */
3   1      v58
4   1      003
5   0      v58
;

proc transpose data=have out=want(drop=_name_) prefix=code_;
  by id;
  id code;   * column name becomes <prefix><code>;
  var found;
run;

* missing occurs when an id was not diagnosed with a code;
* if that means the flag should be zero (for logistic modeling perhaps)
* the missings need to be changed to zeroes;

data want;
  set want;
  array codes code_:;
  do _n_ = 1 to dim(codes);   /* repurpose automatic variable _n_ for loop index */
    if missing(codes(_n_)) then codes(_n_) = 0;
  end;
run;

这篇关于SAS LOOP - 从具有值的记录中创建列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆