What does the upperletter D mean ? and why is the code wrong? Thank you.
With pictures it is hard to tell but it appears to me that there is a space between the " and the D
Examine the lines below:
"01JAN1960" D "01JAN1960"D
The first would be an error, the second is a date literal. Date literal is the way that you tell SAS you want to use the internal numeric representation of a date (or datetime or time value). The D could be lower case as could the month abbreviation. But there cannot be a space between the quote and the letter.
Example log from running code involving those two lines:
58 data junk; 59 x="01JAN1960" D; - 22 ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *, **, +, -, /, <, <=, <>, =, >, ><, >=, AND, EQ, GE, GT, IN, LE, LT, MAX, MIN, NE, NG, NL, NOTIN, OR, ^=, |, ||, ~=. 60 run; NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column). 59:6 NOTE: The SAS System stopped processing this step because of errors. WARNING: The data set WORK.JUNK may be incomplete. When this step was stopped there were 0 observations and 2 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.01 seconds 61 62 data junk2; 63 x="01JAN1960"D; 64 run; NOTE: The data set WORK.JUNK2 has 1 observations and 1 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds
The underscore under the letter D in the first bit is "unexpected" because of the space.
Pictures of proportional font text can be hard to tell but I think that is the issue in this case.
If you copy and run my code you will find the value of X in the Junk2 data set is indeed 0. 1 January 1960 is the base numeric value for dates and is day "zero".
The D indicates that SAS will convert the value inside the double-quotes to a valid SAS date. SAS dates are integers, representing the number of days since 01JAN1960, so the correct value is 0 (if there are no syntax errors). Had the code read
x="01JAN2020"d;
the value would be 21915, the number of days since 01JAN1960.
It's a "constant" (could have sworn it was once called literal) and tells SAS to interpret the data in quotes as a date.
Some of the other options you may see:
DT -> Datetime
T -> Time
N -> Variable or data set name
This also means if you add code right after the end of the quotation marks you can sometimes get a message about the behaviour of a variable after a quote changing over time.
Characters That Follow a Character Constant |
Possible Interpretations |
Examples |
---|---|---|
b |
bit testing constant |
'00100000'b |
d |
date constant |
'01jan04'd |
dt |
datetime constant |
'18jan2005:9:27:05am'dt |
n |
name literal |
'My Table'n |
t |
time constant |
'9:25:19pm't |
x |
hexadecimal notation |
'534153'x |
@tianerhu wrote:
What does the upperletter D mean ? and why is the code wrong? Thank you.
With pictures it is hard to tell but it appears to me that there is a space between the " and the D
Examine the lines below:
"01JAN1960" D "01JAN1960"D
The first would be an error, the second is a date literal. Date literal is the way that you tell SAS you want to use the internal numeric representation of a date (or datetime or time value). The D could be lower case as could the month abbreviation. But there cannot be a space between the quote and the letter.
Example log from running code involving those two lines:
58 data junk; 59 x="01JAN1960" D; - 22 ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *, **, +, -, /, <, <=, <>, =, >, ><, >=, AND, EQ, GE, GT, IN, LE, LT, MAX, MIN, NE, NG, NL, NOTIN, OR, ^=, |, ||, ~=. 60 run; NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column). 59:6 NOTE: The SAS System stopped processing this step because of errors. WARNING: The data set WORK.JUNK may be incomplete. When this step was stopped there were 0 observations and 2 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.01 seconds 61 62 data junk2; 63 x="01JAN1960"D; 64 run; NOTE: The data set WORK.JUNK2 has 1 observations and 1 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds
The underscore under the letter D in the first bit is "unexpected" because of the space.
Pictures of proportional font text can be hard to tell but I think that is the issue in this case.
If you copy and run my code you will find the value of X in the Junk2 data set is indeed 0. 1 January 1960 is the base numeric value for dates and is day "zero".
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.