在 SAS 数据步骤 monyy5 中将日期转换为字符串。到yymmn6

问题描述 投票:0回答:3

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

JUN18
'201806'

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

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

结果是

new_b
=
JUN18
。我尝试了相反的方向,但有些东西刚刚消失,我不知道我错过了什么。有谁知道如何转换这些数据类型?

谢谢。

sas enterprise-guide
3个回答
2
投票

使用

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=;

0
投票

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

您只需要切换它们即可。

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

0
投票

由于缺乏声望点而无法发表评论...

@Reeza 在 ATTRIB 语句中使用 FORMAT 语句或 FORMAT 子句不会将数字字段更改为字符格式。它们用于将值的视图重新分配为基于字符或基于数字的“格式”,但不会更改核心数据。数据将保持数字状态,直到使用 PUT 或 PUTN 语句将结果值分配到新的或现有的字符变量中进行更改,按照 Richard 的回答。

© www.soinside.com 2019 - 2024. All rights reserved.