Convert Date to String in SAS data step monyy5. to yymmn6

雨燕双飞 提交于 2020-01-14 05:23:11

问题


In SAS EG, in a data step, I am trying to convert from a date to a string in the following formats:

JUN18 to '201806'

I am able to convert the opposite direction using a data step as follows:

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

The result is that new_b = JUN18. I have tried the opposite direction and something is just off and I can't figure out what I am missing. Does anyone know how to convert these data types?

Thanks.


回答1:


Use the PUT or PUTN function to convert a SAS date value to a string containing a date representation.

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;

The VVALUE function can be used to obtain the formatted value (the data value representation in a character string) of a variable using its current format attribute.

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



回答2:


You need to switch your formats and informats. In the INPUT function, you put the format your data looks like, in this case, monyy5 and then the format is what you want your data to look like, in this case, YYMMN6.

Switching them is all you need.

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


来源:https://stackoverflow.com/questions/54334541/convert-date-to-string-in-sas-data-step-monyy5-to-yymmn6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!