SAS中文论坛

标题: 关于数据集的转换 [打印本页]

作者: shiyiming    时间: 2009-8-4 17:30
标题: 关于数据集的转换
[b:2ov75lyu]我有如下数据data aa;
input a b c d;
cards;
1 . . .
. 2 . .
. . 3 .
. . . 4
;
run;期望转化为a b c d
1 2 3 4 变量可能有100个而且类型可能有字符型 这里只是举个例子[/b:2ov75lyu]
作者: shiyiming    时间: 2009-8-4 20:52
标题: Re: 关于数据集的转换
没太看明白,是这意思不? 效率不太高,凑合看吧.
[code:24v3c1vt]data aaa;
        input a b v_c $ d e v_f $;
cards;
1 . . . . .
. 2.22222 . . . .
. . C . . .
. . . 4.44444 . .
. . . . 5 .
. . . . . F
;

%macro convert(inds,outds);
        proc contents data=&inds out=_temp(keep=name type varnum) short noprint;
        run;
        proc sort data=_temp;
                by varnum;
        run;
        data _temp;
                set _temp end=last;
                retain var_list var_type;
                length var_list var_type $32767;
                var_list=catx(',',var_list,name);
                var_type=catx(',',var_type,put(type,8.));
                if last then do;
                        call symput('var_list',strip(var_list));
                        call symput('type_list',strip(var_type));
                        call symput('var_num',strip(put(_n_,8.)));
                end;
        run;
        proc datasets library=work nolist;
                delete _temp;
        run;
        data &outds;
                set &inds end=last;
                array arr{&var_num} $ _temporary_;
                %do i=1 %to &var_num;
                        %if %scan(%quote(&type_list),&i)=1 %then %do;
                                if _n_=&i then arr(&i)=put(%scan(%quote(&var_list),&i),8.);
                        %end;
                        %else %do;
                                        if _n_=&i then arr(&i)=%scan(%quote(&var_list),&i);
                                %end;
                %end;
                if last then do;
                        %do i=1 %to &var_num;
                                %if %scan(%quote(&type_list),&i)=1 %then %do;
                                        %scan(%quote(&var_list),&i)=input(arr(&i),8.);
                                %end;
                                %else %do;
                                                %scan(%quote(&var_list),&i)=arr(&i);
                                        %end;
                        %end;
                        output;       
                end;
        run;
%mend;

%convert(work.aaa,work.bbb)[/code:24v3c1vt]




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