如何将char数据类型转换为sas中的数值数据类型

时间:2015-01-30 07:35:08

标签: mysql sas

我有一个表格,其描述如下所示:

create table WORK.EMP( bufsize=65536 )
(

    Series_Name char(21) format=$21. informat=$21.,
    Series_Code char(17) format=$17. informat=$17.,
    Country_Name char(19) format=$19. informat=$19.,
    Country_Code char(3) format=$3. informat=$3.,
    _2000__YR2000_ char(12) format=$12. informat=$12.,
    _2001__YR2001_ char(12) format=$12. informat=$12.,
    _2002__YR2002_ char(12) format=$12. informat=$12.,
    _2003__YR2003_ char(12) format=$12. informat=$12.,
    _2004__YR2004_ char(11) format=$11. informat=$11.,
    _2005__YR2005_ char(11) format=$11. informat=$11.,
    _2006__YR2006_ char(11) format=$11. informat=$11.,
    _2007__YR2007_ char(12) format=$12. informat=$12.,
    _2008__YR2008_ char(12) format=$12. informat=$12.,
    _2009__YR2009_ char(12) format=$12. informat=$12.,
    _2010__YR2010_ char(12) format=$12. informat=$12.,
    _2011__YR2011_ char(12) format=$12. informat=$12.,
    _2012__YR2012_ char(12) format=$12. informat=$12.,
    Total12 num label='Total12'
   );

现在我想总结从2000年到2012年的列,并将结果放入名为' Total12'的列中,但首先我认为应该转换' char'数据类型分为' int'做计算。我在SAS中尝试了以下代码:

proc sql;    
alter table emp modify _2000__YR2000_ num;    
alter table emp modify _2001__YR2001_ num;    
run;

但它给了我以下错误:

  

错误:你不能改变' _2000__YR2000 _'是一个数字列。

我现在该怎么办?

3 个答案:

答案 0 :(得分:1)

一种方法是在数据步骤中使用输入语句来创建一个名为不同的新变量/列,删除原始变量/列,然后重命名新变量以匹配原始变量。

data work.want(rename=(_2000_YR2000_b=_2000_YR2000_));
   set work.emp;
  _2000__YR2000_b=input(_2000__YR2000_,8.);
  drop _2000_YR2000_;
run;

等等。

答案 1 :(得分:1)

这应该适合你:

data have;
_2000__YR2000_ = "1";
_2001__YR2001_ = "2";
_2002__YR2002_ = "3";
_2003__YR2003_ = "4";
_2004__YR2004_ = "5";
_2005__YR2005_ = "6";
_2006__YR2006_ = "7";
_2007__YR2007_ = "8";
_2008__YR2008_ = "9";
_2009__YR2009_ = "10";
_2010__YR2010_ = "11";
_2011__YR2011_ = "12";
_2012__YR2012_ = "13";
run;

data want;
set have;
array years[*]
_2000__YR2000_ 
_2000__YR2000_
_2001__YR2001_ 
_2002__YR2002_
_2003__YR2003_ 
_2004__YR2004_ 
_2005__YR2005_
_2006__YR2006_
_2007__YR2007_ 
_2008__YR2008_
_2009__YR2009_ 
_2010__YR2010_
_2011__YR2011_ 
_2012__YR2012_ 
;
total = sum( of years[*]);
run;

答案 2 :(得分:1)

如果变量名称状态良好,则不需要宏。

data test;
    set have;
    array year20{*} _20:;
    call missing(year);
    do i = 1 to dim(year20);
        year = sum(year, input(year20{i}, ?? best.));
    end;
run;