SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 750|回复: 1
打印 上一主题 下一主题

改变sas数据集中变量的属性

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2004-4-14 12:39:36 | 只看该作者

改变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.);
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
沙发
 楼主| 发表于 2004-4-15 09:44:34 | 只看该作者
/* 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]
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|SAS中文论坛  

GMT+8, 2025-1-9 23:55 , Processed in 0.280493 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表