SAS中文论坛

标题: 变量值的选择 [打印本页]

作者: shiyiming    时间: 2011-7-24 22:54
标题: 变量值的选择
具体例题如下:
[code:1ptpplgi]data w;
input id        x1        x2        x3        x4        x5 ;
cards;
1        15        .        16        21        23
2        16        18        20        23        25
3        15        .        16        21        23
4        16        18        20        23        25
5        14        15        16        18        22
6        13        16        17        18        20
7        15        16        17        18        19
8        12        13        14        15        16
9        11        15        17        19        21
10        10        11        13        14        16
11        9        10        15        16        19
12        16        17        18        24        15
13        17        18        19        22        23
14        8        10        12        14        19
15        11        13        14        15        18
;[/code:1ptpplgi]
[b:1ptpplgi]生成一个新变量--new,期望在x1-x5之间选择其取值16,若某个观测没有16 ,则取其最接近的值,但是若有两个取值时(15&17,14&18),则取15,14等左边值,[/b:1ptpplgi]
先谢谢了,期待大侠的回复!!
作者: shiyiming    时间: 2011-7-25 14:00
标题: Re: 变量值的选择
首先,oloolo楼主对类似情况有研究。
替换缺失值后,试试这个:
[code:2yofm0sa]data n (drop=_:);
    array _v[5] x1-x5;
    set w;by id;
    _diff=constant("big");
    do _i=1 to 5;
    _ndiff=abs(_v[_i]-16);
    if _ndiff=>_diff then continue;
    _diff=_ndiff;
    x6=_v[_i];
    end;
run;
proc print;run;[/code:2yofm0sa]
作者: shiyiming    时间: 2011-7-30 08:23
标题: Re: 变量值的选择
接着前面的问题,数据集若下:
[code:joc2p2zl]data j;
input  id x1-x5 y1-y5;
cards;
1        15        .        16        21        23        .        65        47        45        65
2        16        18        20        23        25        52        53        54        55        56
3        15        .        16        21        23        48        48        .        51        51
4        16        18        20        23        25        68        68        68        68.5        70
5        14        15        16        18        22        75        76        76        77        77.5
6        13        16        17        18        20        .        .        47        45        65
7        15        16        17        18        19        52        53        54        55        56
8        12        13        14        15        16        48        48        49        51        51
9        11        15        17        19        21        68        68        68        68.5        70
10        10        11        13        14        16        75        76        76        77        77.5
11        9        10        15        16        19        50        .        47        45        65
12        16        17        18        24        15        52        53        54        55        56
13        17        18        19        22        23        48        48        49        51        51
14        8        10        12        14        19        68        68        68        68.5        70
15        11        13        14        15        18        75        76        76        .        77.5
;
run;[/code:joc2p2zl]
[color=#FF0000:joc2p2zl]先说明如下,x1-x5是与y1-y5对应,即x1-y1,x2-y2,........x5-y5;意思就是选择x1的时候同时选择y1,选择x3的时候同时选择y3,其余与前面的条件一样,再次期待回复~~~[/color:joc2p2zl]
作者: shiyiming    时间: 2011-7-30 08:55
标题: Re: 变量值的选择
数据的缺失值是?在上面的代码上加入一个数组,不知道这样理解对不对(缺失值存在影响结果)
[code:1zake3zp]data n (drop=_:);
    array _v[5] x1-x5;
        array _w[5] y1-y5;
    set j;by id;
    _diff=constant("big");
    do _i=1 to 5;
    _ndiff=abs(_v[_i]-16);
    if _ndiff=>_diff then continue;
    _diff=_ndiff;
    x6=_v[_i];
        y6=_w[_i];
    end;
run;
proc print;run;[/code:1zake3zp]
作者: shiyiming    时间: 2011-8-1 21:03
标题: Re: 变量值的选择
加上一个判断- if not missing(_v(_i)) then do;.....;end;

data n2 (drop=_:);
    array _v[5] x1-x5;
    set w;by id;
    _diff=constant("big");
    do _i=1 to 5;
        if not missing(_v(_i)) then do;
            _ndiff=abs(_v[_i]-16);
            if _ndiff=>_diff then continue;
            _diff=_ndiff;
            x6=_v[_i];
         end;
    end;
run;
作者: shiyiming    时间: 2012-9-22 23:59
标题: Re: 变量值的选择
[quote="sun59338":c43tps2u]加上一个判断- if not missing(_v(_i)) then do;.....;end;

data n2 (drop=_:);
    array _v[5] x1-x5;
    set w;by id;
    _diff=constant("big");
    do _i=1 to 5;
        if not missing(_v(_i)) then do;
            _ndiff=abs(_v[_i]-16);
            if _ndiff=>_diff then continue;
            _diff=_ndiff;
            x6=_v[_i];
         end;
    end;
run;[/quote:c43tps2u]
<!-- s:D --><img src="{SMILIES_PATH}/icon_biggrin.gif" alt=":D" title="Very Happy" /><!-- s:D --> 谢谢,受用了~~~




欢迎光临 SAS中文论坛 (http://www.mysas.net/forum/) Powered by Discuz! X3.2