SAS宏变量作为差异日期信息/格式
我想将一个日期作为变量,我想转换为其他格式(SAS线人)。 我需要那些与表名称相连的炭。
%LET GL_YMD= %sysfunc(mdy(1,31,2009),date9.);/*expeced result GL_YMD=31JAN2009*/
%LET YYYYMMDD= %sysfunc(inputn(&GL_YMD,YYMMDD8.)); /*expeced result YYYYMMDD=20090131*/
%LET YYYYMM= %sysfunc(inputn(&GL_YMD,YYYYMM)); /*expeced result YYYYMM=200901*/
我读了很多有关此主题的教程,看来它应该起作用,但是它: 我得到一个回应: 注49-169:标识符引用字符串后的含义可能会在未来的SAS版本中发生变化。插入空白 建议在引用的字符串和后续标识符之间进行。
I want to put one date as a variable and I want to convert to a different format (sas informant).
I need those to become a char to concatenate with the table name.
%LET GL_YMD= %sysfunc(mdy(1,31,2009),date9.);/*expeced result GL_YMD=31JAN2009*/
%LET YYYYMMDD= %sysfunc(inputn(&GL_YMD,YYMMDD8.)); /*expeced result YYYYMMDD=20090131*/
%LET YYYYMM= %sysfunc(inputn(&GL_YMD,YYYYMM)); /*expeced result YYYYMM=200901*/
I read lots of tutorials on this topic, it seems it should work but it:
I get a response:
NOTE 49-169: The meaning of an identifier after a quoted string might change in a future SAS release. Inserting white space
between a quoted string and the succeeding identifier is recommended.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
inputn()
将字符转换为数字。您要改用putn()
,并将日期指定为日期文字,”& gl_ymd“ d
。您已将日期转换为字符date9格式,这些功能预计SAS日期编号。请注意,您还应将
yymmddn8。
用作您的和yymmn6。
作为您的格式以获取预期结果。“& gl_ymd” d
等同于:请注意,这是一种特殊情况。该功能可以正确地解决此问题,因为它旨在解析日期文字,但是就宏编译器而言,它只是文本。如果将
“& gl_ymd” d
放在日志中,则将完全看到。这可能是一个令人困惑的规则,但要知道宏中的日期功能很有帮助。inputn()
converts characters to a number. You want to useputn()
instead, and specify the date as a date literal,"&GL_YMD"d
. You've converted your date to a character date9 format, and these functions expect a SAS date number.Note you should also be using
yymmddn8.
as your andyymmn6.
as your formats to get your expected results."&GL_YMD"d
is equivalent to:Note that this is a special case. The function can work this out correctly because it is designed to parse date literals, but as far as the macro compiler is concerned, it's just text. If you put
"&GL_YMD"d
in the log, you will see exactly that. It can be a confusing rule but it's helpful to know for date functions in macros.没有得到便条,但我确实从Inputn函数中收到了警告。您需要putn函数。注意使用日期的使用。
Did not get the NOTE but I did get warnings from INPUTN function. You need PUTN function. Note the use of date literal.
格式将值转换为文本。您可以使用put(),putn()和putc()函数以及put和格式语句的格式。数字格式将数字转换为文本。字符格式将字符值转换为文本。
信息将文本转换为值。您可以将Informats与INPUT(),Inputn()和Inputc()函数以及输入和信息语句一起使用。数字信息将文本转换为数字。字符信息将文本转换为字符字符串。
您首先使用mdy()函数和date9。格式规范以创建字符串31Jan2009。然后,您尝试使用inputn()函数来解释该字符串,就像它是YYMMDD Informat识别的样式一样。但是该信息将不理解扬的信。即使工作过,它也将返回用来表示该日期的内部数量,而不是您声称想要的风格化的yyyymmdd字符串。
因此,使用格式而不是信息使用putn()函数。将在样式yyyymmdd中制成字符串的格式规范是yymmddn8。 Yymmn6将以Yyyymm的样式制作字符串的格式。格式。
结果:
如果您想让SAS解释字符串31JAN2009为日期值,则需要将其转换为日期(引用的字符串),该字符串可以立即解释日期信息,然后立即解释字母D)。因此,您还可以使用gl_ymd中的字符串来表示这样的日期:
Formats convert values to text. You use formats with the PUT(), PUTN(), and PUTC() functions and the PUT and FORMAT statements. Numeric formats convert numbers to text. Character formats convert character values to text.
Informats convert text to values. You use informats with the INPUT(), INPUTN(), and INPUTC() functions and the INPUT and INFORMAT statements. Numeric informats convert text to numbers. Character informats convert text to character strings.
You first used the MDY() function and the DATE9. format specification to create the string 31JAN2009. You then tried to use the INPUTN() function to interpret that string as if it was in the style recognized by the YYMMDD informat. But that informat will not understand the letter JAN. And even if had worked it would have returned the internal number of days used to represent that date, and not the stylized YYYYMMDD string you claimed to want.
So use the PUTN() function instead with a FORMAT instead of an INFORMAT. The FORMAT specification that will make a string in the style YYYYMMDD is YYMMDDN8. and the format that will make a string in the style YYYYMM is the YYMMN6. format.
Result:
If you want to have SAS interpret the string 31JAN2009 as a date value you need to convert it to a date literal (a quoted string that the DATE informat can interpret followed immediately by the letter d). So you could also use the string in GL_YMD to represent a date like this: