如何将日期字段转换为这种格式 2018 / 01

How to convert date field to this format 2018 / 01

我的 table 中有一个日期字段作为 char "01jan2017",

我想将日期字段转换为这种格式 "2018 / 01" 并且正斜杠之间应该有 space。

谢谢

如果您要找的只是为了显示,那么这里是一个字符转换

data r;
  date = '01jan2017'd;
  date1 = compbl(put(year(date),best.)|| " / "||put(month(date),z2.));
run;

您需要完成三个关键步骤:

catx(' / ',year(input(date_char,date9.)),put(month(input(date_char,date9.)),z2.));
  1. 将日期转换为date9.格式,以便提取年月,
  2. 月份使用 z2. 格式以获得前导零,
  3. 使用Catx()连接年月&'/'。

完整代码:

data want;
date_char="01jan2017";
dateYYMM=catx(' / ',year(input(date_char,date9.)),put(month(input(date_char,date9.)),z2.));
run;

输出:

 date_char=01jan2017 dateYYMM=2017 / 01