Hi! Below is an excerpt of my data step. I am trying to write if-then logic such that if a particular condition is met (in this case if there is a particular record ID, then the variable, qi100d is a particular date:
data four;
set three;
if record_id="CABA010" then
qi100a=upcase(redcap_data_access_group)
and qi100b="C-ABA-DQQ-000001"
and qi100d="2-22-2024"d
However, SAS detects an error for this excerpt. The errors are as follows:
464 data four;
465 set three;
466 if record_id="CABA010" then
467 qi100a=upcase(redcap_data_access_group)
468 and qi100b="C-ABA-DQQ-000001"
469 and qi100d="2-22-2024"d
------------
77
ERROR: Invalid date/time/datetime constant "2-22-2024"d.
ERROR 77-185: Invalid number conversion on "2-22-2024"d.
Is there a particular reason why this error is popping up? Any input regarding this would be much appreciated; thanks so much!
469 and qi100d="2-22-2024"d ------------ 77 ERROR: Invalid date/time/datetime constant "2-22-2024"d.
This is not a format issue. It is, as stated in the ERROR message, an invalid date/time/datetime constant. A valid date constant for Feb 22, 2024 is '22FEB2024'd, exactly like that, no changes allowed, except FEB can be lower case. It can't have dashes or slashes or blanks, it must be as I just described, with a two digit day of month, followed by a three character month, followed by a 4 digit year.
469 and qi100d="2-22-2024"d ------------ 77 ERROR: Invalid date/time/datetime constant "2-22-2024"d.
This is not a format issue. It is, as stated in the ERROR message, an invalid date/time/datetime constant. A valid date constant for Feb 22, 2024 is '22FEB2024'd, exactly like that, no changes allowed, except FEB can be lower case. It can't have dashes or slashes or blanks, it must be as I just described, with a two digit day of month, followed by a three character month, followed by a 4 digit year.
Hi @JackZ295,
In addition to the invalid date literal to be corrected, the three assignment statements must be enclosed in a DO-END block, not connected with AND.
if record_id="CABA010" then do;
qi100a=upcase(redcap_data_access_group);
qi100b="C-ABA-DQQ-000001";
qi100d="22FEB2024"d;
end;
@PaigeMiller: Blanks and various special characters are allowed in a date literal (albeit not recommended), even text following the year component.
705 data _null_; 706 d1='***1\_.~^~._/Jan:-)24==>Happy New Year!'d; 707 put d1; 708 d2='01JAN2024'd; 709 put d2; 710 run; 23376 23376 NOTE: DATA statement used (Total process time):
@FreelanceReinh wrote:
@PaigeMiller: Blanks and various special characters are allowed in a date literal (albeit not recommended), even text following the year component.
Spoiler705 data _null_; 706 d1='***1\_.~^~._/Jan:-)24==>Happy New Year!'d; 707 put d1; 708 d2='01JAN2024'd; 709 put d2; 710 run; 23376 23376 NOTE: DATA statement used (Total process time):
I'll go further than "not recommended". The documentation doesn't say this is allowed, so it is undocumented and could stop working in the next release or next hot-fix, and thus it is unwise to use. https://documentation.sas.com/doc/en/lrcon/9.4/p0cq7f0icfjr8vn19vyunwmmsl7m.htm#n1bbnuc86c5c5hn113qb...
@PaigeMiller The most recent docu for SAS9.4 Date, Time, and Datetime Constants allows for the following string patterns.
The way I normally explain it is that the string inside the quotes has to be something the DATE informat can read. The DATE11. informat can handle pretty much any valid delimiter between the parts of the string.
The reality is that is actually excepts more than that. Kind of like how the COMMA informat will ignore any number of commas.
A quick search found this help page for date constant syntax.
https://documentation.sas.com/doc/en/pgmsascdc/v_048/proccas/p14t12jhqk5yzmn11oewj4c1ehh2.htm
Testing the examples on that page:
1 data test; 2 date1='1jan2018'D; 3 date2="01jan18"D; 4 time1='9:25'T; 5 time2="9:25:19pm"T; 6 dt1='01may18:9:30:00'DT; 7 dt2="18jan2018:9:27:05am"DT; 8 dt3='2018-07-20T12:00:00Z'DT; 9 dt3='2018-05-17T09:15:30-05:00'DT; 10 format date: date9. time: tod8. dt: datetime19.; 11 put (_all_) (=/); 12 run; date1=01JAN2018 date2=01JAN2018 time1=09:25:00 time2=21:25:19 dt1=01MAY2018:09:30:00 dt2=18JAN2018:09:27:05 dt3=17MAY2018:09:15:30 NOTE: The data set WORK.TEST has 1 observations and 7 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds
if record_id="CABA010" then
qi100a=upcase(redcap_data_access_group) /* you start an assignment here */
and qi100b="C-ABA-DQQ-000001" /* these AND is part of the assignment, so it makes the result of the whole assignment boolean */
and qi100d="2-22-2024"d
Do you really want this?
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.