从术语代码创建年份/在SAS中操作日期字符串

时间:2014-11-08 20:20:55

标签: string date sas

我将期限代码转换为年份值。所以我的术语代码类似于..

data terms ; 
input term_code $; 
cards;
199900
199910
199920
200000
200010
200020
200100
200110
200120
201000
201010
; 
run;

我可以使用以下代码将它们转换为我想要的内容......

data terms_Year ; set terms ; 

if(Substr ( Term_Code, 1, 4 ) >= '2010')
   then Year = Substr ( Term_Code, 1, 4 ) || '-1' || ( 1  *  Substr ( Term_Code, 4,1 ) + 1 );
else if(Substr ( Term_Code, 1, 4 ) >= '2009')
   then year = Substr ( Term_Code, 1, 4 ) || '-'  || ( 1  *  Substr ( Term_Code, 4,1 ) + 1 );
else if(Substr ( Term_Code, 1, 4 ) >= '2000')
   then year = Substr ( Term_Code, 1, 4 ) || '-0' || ( 1  *  Substr ( Term_Code, 4,1 ) + 1 );
else if(Substr ( Term_Code, 1, 4 ) =  '1999')
   then year = Substr ( Term_Code, 1, 4 ) || '-00';
else    year = Substr ( Term_Code, 1, 4 ) || '-'  ||  ( 1  *  Substr ( Term_Code, 3,2 ) + 1 );
        year = compress(year,,'s');
;
run;

proc print data=terms_year ; run; 

这是令人满意的,但我不能帮助但是好奇一位经验丰富的SAS程序员如何解决这样的问题。谢谢你的时间。

2 个答案:

答案 0 :(得分:0)

我认为这是一个更易读的选择,而且非常简单。不完全是我正在考虑的格式/信息的东西..而且必须创建/然后删除变量有点烦人。

data terms_Year ; set terms ; 
drop year_0 year_1 ; 

if(Substr ( Term_Code, 1, 4 ) >= '2010')
   then Year = Substr ( Term_Code, 1, 4 ) || '-1' || ( 1  *  Substr ( Term_Code, 4,1 ) + 1 );
else if(Substr ( Term_Code, 1, 4 ) >= '2009')
   then year = Substr ( Term_Code, 1, 4 ) || '-'  || ( 1  *  Substr ( Term_Code, 4,1 ) + 1 );
else if(Substr ( Term_Code, 1, 4 ) >= '2000')
   then year = Substr ( Term_Code, 1, 4 ) || '-0' || ( 1  *  Substr ( Term_Code, 4,1 ) + 1 );
else if(Substr ( Term_Code, 1, 4 ) =  '1999')
   then year = Substr ( Term_Code, 1, 4 ) || '-00';
else    year = Substr ( Term_Code, 1, 4 ) || '-'  ||  ( 1  *  Substr ( Term_Code, 3,2 ) + 1 );
        year = compress(year,,'s');

year_0 = compress(substr(term_code,1,4)); 
year_1 = compress(year_0 + 1 );
year_also = catx('-',year_0, substr(year_1,3,2));
;
run;

proc print data=terms_year ; run; 

答案 1 :(得分:0)

使用一些函数代替,因为看起来年份是前4个字符,然后你可以添加1来获得第二年并截断它以获得最后两个字符。

data terms_Year ; 
    set terms ; 
    year2=input(substr(year,1, 4), 4.);
    Year_want=catx("-", put(year2, 4. -l), substr(put(year2+1, 4. -l), 3));
run;