在 SAS EG 中,在数据步骤中,我尝试将日期转换为以下格式的字符串:
JUN18
至 '201806'
我可以使用数据步骤转换相反的方向,如下所示:
data date;
length b $6;
b='201806';
new_b=input(b, yymmn6.);
format new_b monyy5.;
结果是
new_b
= JUN18
。我尝试了相反的方向,但有些东西刚刚消失,我不知道我错过了什么。有谁知道如何转换这些数据类型?
谢谢。
使用
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=;
您需要切换格式和信息。在 INPUT 函数中,输入数据的格式(在本例中为 monyy5),然后格式为您希望数据的格式(在本例中为 YYMMN6)。
您只需要切换它们即可。
data date;
length b $6;
b='Jun18';
new_b=input(b, monyy5.);
format new_b yymmn6.;
run;