【问题标题】:Convert Date to String in SAS data step monyy5. to yymmn6在 SAS 数据步骤 monyy5 中将日期转换为字符串。到 yymmn6
【发布时间】:2019-01-23 19:41:40
【问题描述】:

在 SAS EG 中,在数据步骤中,我尝试将日期转换为以下格式的字符串:

JUN18 到 '201806'

我可以使用如下数据步骤转换相反的方向:

data date;
length b $6;
b='201806';
new_b=input(b, yymmn6.);
format new_b monyy5.;

结果是new_b = JUN18。我尝试了相反的方向,但有些东西刚刚关闭,我无法弄清楚我错过了什么。有谁知道如何转换这些数据类型?

谢谢。

【问题讨论】:

  • 原始变量类型和格式是什么,在这种情况下 B 是源数据中的字符?

标签: sas enterprise-guide


【解决方案1】:

使用PUT 或PUTN 函数将SAS 日期值转换为包含日期表示的字符串。

data _null_;

  mydate = '18JUN2018'D;  
  * variable is numeric and contains a SAS date value;

  format mydate monyy.; 
  * variable given a format that is used when value is output (PROC rendered or PUT);

  put mydate=;  
  * the LOG will show JUN18, mydate is still a numeric holding a SAS date value;

  mydate_str = put (mydate, yymmN.);  
  * put returns the formatted value using yymmN representation of the data value;
  * yymmN format is different than monyy format associated with the variable,
  * and thus this is the 'conversion';

  put mydate_str=; 
  * the LOG will show 201806, mydate_str is a $6 variable and can not be used in date value computations;

run;

VVALUE 函数可用于使用其当前格式属性获取变量的格式化值(字符串中的数据值表示形式)。

  length my_date_formatted_str $10;
  mydate_formatted_str = vvalue(mydate);
  put mydate_formatted_str=;

【讨论】:

    【解决方案2】:

    您需要切换格式和信息。在 INPUT 函数中,输入数据的格式,在本例中为 monyy5,然后格式为您希望数据的格式,在本例中为 YYMMN6。

    你只需要切换它们。

    data date;
    length b $6;
    b='Jun18';
    new_b=input(b, monyy5.);
    format new_b yymmn6.;
    run;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-30
      • 2015-12-13
      • 1970-01-01
      相关资源
      最近更新 更多