SAS Base (55)

The following SAS program is submitted:

data WORK.DATE_INFO;
X=”01Jan1960″D ;
run;

Variable X contains what value?

A. the numeric value 0
B. the character value “01Jan1960”
C. the date value 01011960
D. the code contains a syntax error and does not execute.

Check Answer
Answer: A

注解:Letter D is used to convert a normal date in DDMMMYY or DDMMMYYYY format to SAS date value. Check SAS Base (16) for details.

This question has another version. A space is placed between the closing quotation mark and letter D. This will cause a compile error and the answer would be D.

SAS Base (16)

The following SAS program is submitted:

data WORK.DATE_INFO;
X=’04jul2005’d;
DayOfMonth=day(x);
MonthOfYear=month(x);
Year=year(x);
run;

What types of variables are DayOfMonth, MonthOfYear, and Year?

A. DayOfMonth, Year, and MonthOfYear are character.
B. DayOfMonth, Year, and MonthOfYear are numeric.
C. DayOfMonth and Year are numeric. MonthOfYear is character.
D. DayOfMonth, Year, and MonthOfYear are date values.

Check Answer
Answer: B

注解:d的作用是将“数字day + 英文month前三个字母 + 数字year”格式的日期,转化成SAS日期,即以1960年1月1日为0,2005年7月4日则为16621。DAY, MONTH, YEAR这三个function则是分别返回一个SAS格式日期的日,月,年,并且返回的值均为数值型。

SAS Base (4)

The following SAS program is submitted:

data WORK.DATE_INFO;
Month=”01″ ;
Yr=1960 ;
X=mdy(Month,01,Yr) ;
run;

What is the value of the variable X?
A. the numeric value 0
B. the character value “01011960”
C. a missing value due to syntax errors
D. the step will not compile because of the character argument in the mdy function.

Check Answer
Answer: A

注解:SAS在存储日期时,将1960年1月1日作为基准日期,以数字0表示,1959年12月31日为-1,1960年1月2日为1,以此类推。MDY这个function的作用是将某一日期转化为SAS日期。1960年1月1日正是基准日期,所以X为数字0.值得注意的是,题目中的Month是字符型(Char)变量而非数字,在程序执行过程中,SAS将char “01”转化成了数字01,并在Log中通过Note予以显示:”Character values have been converted to numeric values at the places given by:”