SAS中文论坛

标题: 求助: 怎样替换变量的值? [打印本页]

作者: shiyiming    时间: 2011-11-29 12:41
标题: 求助: 怎样替换变量的值?
我有两个表,一个小表,它的结构是:
old_intervention_code        new_intervention_code
1abc                                   1efg
5cde                                        5xyz
另一个大表有非常多的变量和非常多的观测,而且就intervention_code变量来说,它有20个这样的变量,即intervention_code_01 - intervention_code_20。
小表中的old_intervention_code的值会出现在这01 到20的变量中。我想做的事情是把大表中的,与小表的old_intervention_code值相同的intervention_code_{i}都换成小表中对应的new_intervention_code的值。

我写了一点,但是发现并没有实现替换。请各位高手帮我看看。非常感谢!
data intervention_code_replaced;
set code_map (obs=1);
declare hash ht (hashexp:16, dataset: "code_map");
        ht.definekey('old_intervention_code');
        ht.definedata('new_intervention_code');
        ht.definedone();
do until (eof);

  set thebigtable end=eof;

    if ht.find()=0 then do;
      ht.replace();
     output intervention_code_replaced;
    end;end;
stop;
run;
作者: shiyiming    时间: 2011-11-29 19:28
标题: Re: 求助: 怎样替换变量的值?
[code:2pgjskvx]data code_map;
    input old_intervention_code $ new_intervention_code $;
datalines;
1abc 1efg
5cde 5xyz
;
data thebigtable;
    input (intervention_code_01-intervention_code_03) (3*:$8.);
datalines;
1abc 5cde 2bbb
5cde 2bbb 1abc
;
data thebigtable;
    length old_intervention_code new_intervention_code $8;
    if _n_=1 then do;
        declare hash ht (hashexp:16, dataset: "code_map");
        ht.definekey('old_intervention_code');
        ht.definedata('new_intervention_code');
        ht.definedone();
        call missing(old_intervention_code,new_intervention_code);
    end;
    set thebigtable;
    array code $8 intervention_code_01-intervention_code_03;
    do over code;
        if ht.find(key:code)=0 then code=new_intervention_code;
    end;
    drop old_intervention_code new_intervention_code;
run; [/code:2pgjskvx]
作者: shiyiming    时间: 2011-11-29 21:59
标题: Re: 求助: 怎样替换变量的值?
Great! Thank you so much!
作者: shiyiming    时间: 2011-11-29 22:59
标题: Re: 求助: 怎样替换变量的值?
[quote="hopewell":1cqklnd5][code:1cqklnd5]data code_map;
    input old_intervention_code $ new_intervention_code $;
datalines;
1abc 1efg
5cde 5xyz
;
data thebigtable;
    input (intervention_code_01-intervention_code_03) (3*:$8.);
datalines;
1abc 5cde 2bbb
5cde 2bbb 1abc
;
data thebigtable;
    length old_intervention_code new_intervention_code $8;
    if _n_=1 then do;
        set code_map;
        declare hash ht (hashexp:16, dataset: "code_map");
        ht.definekey('old_intervention_code');
        ht.definedata('new_intervention_code');
        ht.definedone();
        call missing(old_intervention_code,new_intervention_code);
    end;
    set thebigtable;
    array code $8 intervention_code_01-intervention_code_03;
    do over code;
        if ht.find(key:code)=0 then code=new_intervention_code;
    end;
    drop old_intervention_code new_intervention_code;
run; [/code:1cqklnd5][/quote:1cqklnd5]

Hi hopewell:
I just found that I need to add a set statement after the "if _n_ =1 then do;" line. Otherwise, SAS complains the variables are uninitialized.
And I have run this program, it works perfectly!
Thank you so much!
作者: shiyiming    时间: 2011-12-8 00:20
标题: Re: 求助: 怎样替换变量的值?
Hi Hopewell or fellow SAS users:

Sorry to bother you again.

Right now, I need an update on the code you have provided. That is, in my current small table, the "OLD" column has duplicates, which means, you can see
OLD     NEW
1ABC   2FGH
1ABC   3EST
1ABC   4GJU

And I would perfer to create as many seperate observations as the OLD code needs. For example, in the big or source dataset, there is only one observation that contains the code "1ABC", but I want to create three seperate observations for its replacement, which has "2FGH", "3EST" and "4GJU" respectively.
Right now, the code only find a match and replace once, and it only picks the first matched NEW code. I would like to see all the NEW codes show up in the new dataset.

Thank you very much.
作者: shiyiming    时间: 2011-12-8 22:30
标题: Re: 求助: 怎样替换变量的值?
Thanks everyone.
I used data step "merge" and this issue is resoved.
However, I am still very curious how to use "Hash Object" to deal with the search and replace for duplicated values. If anybody could give me a hint, that will be very helpful.
Thank you very much.
作者: shiyiming    时间: 2011-12-12 23:32
标题: Re: 求助: 怎样替换变量的值?
[code:zo3otjri]data code_map;
    input old_intervention_code $ new_intervention_code $;
datalines;
1abc 1efg
1abc 2fgh
1abc 3est
1abc 4gju
5cde 5xyz
;
data thebigtable;
    input intervention_code $;
datalines;
1abc
5cde
;
data thebigtable;
    length old_intervention_code new_intervention_code $8;
    if _n_=1 then do;
        declare hash h(hashexp:16,dataset:'code_map',multidata:'y');
        h.definekey('old_intervention_code');
        h.definedata('new_intervention_code');
        h.definedone();
        call missing(old_intervention_code,new_intervention_code);
    end;
    set thebigtable;
    if (h.find(key:intervention_code)=0) then do;
        intervention_code=new_intervention_code;
        output;
        do while(h.find_next(key:intervention_code)=0);
            intervention_code=new_intervention_code;
            output;
        end;
    end;
    drop old_intervention_code new_intervention_code;
run; [/code:zo3otjri]
作者: shiyiming    时间: 2011-12-15 04:41
标题: Re: 求助: 怎样替换变量的值?
Thank you very much hopewell!
I really appreciate it!




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