SAS中文论坛

标题: 改变sas数据集中变量的属性 [打印本页]

作者: shiyiming    时间: 2004-4-14 12:39
标题: 改变sas数据集中变量的属性
Character to Numeric:

newvar = INPUT (oldvar, informat);
for example:
NewB = INPUT (VarB, 1.);


Numeric to Character:

newvar = PUT (oldvar, format);
for example:
newD = PUT (VarD, 2.);
作者: shiyiming    时间: 2004-4-15 09:44
/* from <!-- m --><a class="postlink" href="http://www.pauldickman.com/teaching/sas/char_to_num.html">http://www.pauldickman.com/teaching/sas ... o_num.html</a><!-- m --> */

[code:4b0d9]/*test data, ALL character data*/
data chardata;
x='12345';y='0987'; output;
a='3';b='4';output;
run;

/*var names and position*/
proc contents noprint data=chardata out=varname&#40;keep=name npos&#41;;
run;

/* sort by pos in the data set, yields vars
   in the order they appear in the data set*/
proc sort;
by npos;
run;

data _null_;
do i=1 to nobs;
   set varname nobs=nobs end=end;
    call symput&#40;'mac'||left&#40;put&#40;i,4&#46;&#41;&#41;,name&#41;;     /*creates macro variables for name of vars*/
    call symput&#40;'num'||left&#40;put&#40;i,4&#46;&#41;&#41;,'num'||left&#40;put&#40;i,4&#46;&#41;&#41;&#41;;   /*creates new macro numeric vars*/
     if end then call symput&#40;'end',put&#40;nobs,8&#46;&#41;&#41;;   /*determines number of variables, 1 per obs*/
end;         /*assumes none of char vars are larger than 8 bytes*/
run;


/*macro generates the drop and rename, so numeric vars have original names*/
%macro create;
data numdata&#40;drop= %do i=1 %to &end;&&mac&i %end;
            rename=&#40;%do i=1 %to &end; &&num&i=&&mac&i  %end;&#41;&#41;;
     %do i=1 %to &end;
        set chardata ;
        /*create numeric macro vars, which resolve to num1-num5*/
        &&num&i=input&#40;&&mac&i,5&#46;&#41;;
     %end;
run;
%mend;

%create;

/*verify that the new data set contains all numeric vars*/
proc contents data=numdata;
run;[/code:4b0d9]




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