I am trying to use if-then else statement and date constants but getting error message constantly,
data study;
set study;
if missing (Dosedate) then Dose_lot=.;
else if Dosedate ge '14Jan1997'd then dose_lot = 'S0576';
else if Dosedate Lt ‘10Jan1998’d then dose_lot = 'P1122';
else if Dosedate gt '10Jan1998'd then dose_lot = 'P0526';
Can someone pls help me to figure out this mystery?
Thank you
Do you think it might help if we saw the error message?
A couple of problems with your code, two of which would cause errors.
First, the second else if statement contains irregular quotes. Second, you need a length or format statement to define dose_lot as a character variable. e.g.:
data study; input dosedate date9.; cards; 20jan2012 08jan1998 10jan1998 ; data study; set study; length dose_lot $5; if missing (Dosedate) then Dose_lot=.; else if Dosedate ge '14Jan1997'd then dose_lot = 'S0576'; /* else if Dosedate Lt ‘10Jan1998’d then dose_lot = 'P1122';*/ else if Dosedate Lt '10Jan1998'd then dose_lot = 'P1122'; else if Dosedate gt '10Jan1998'd then dose_lot = 'P0526'; run;
However, that said, your last else if condition will NEVER be met as it is already addresed in the first else if statement.
Art, CEO, AnalystFinder.com
@art297 How can you see the irregular quotes in the post? They all look the same to me.
Ok...so you have to copy and paste it into a SAS editor before that shows up.
Or if you post using the code editor then its clear right away that they're different, but not visible if you post directly into the forum.
data study;
set study;
if missing (Dosedate) then Dose_lot=.;
else if Dosedate ge '14Jan1997'd then dose_lot = 'S0576';
else if Dosedate Lt ‘10Jan1998’d then dose_lot = 'P1122';
else if Dosedate gt '10Jan1998'd then dose_lot = 'P0526';
@Reeza: I've attached a copy of how the post appeared in my Chrome browser. Since all of the dates were bold, except for the one in the second else if statement, I figured that it contained some irregular characters.
Art, CEO, AnalystFinder.com
Thank you so much for looking at it. Can you have a look at this attached document. I am still not being able to run the code.
Sorry I wanted to attached this file earlier but forgot to do so.
Then I would use:
data study; set study; length dose_lot $5; if missing (Dosedate) then Dose_lot=.; else if Dosedate gt '10Jan1998'd then dose_lot = 'P0526'; else if year(Dosedate) eq 1997 then dose_lot = 'S0576'; else if year(Dosedate) eq 1998 and Dosedate Le '10Jan1998'd then dose_lot = 'P1122'; run;
However, if that doesn't work, show the code that you used to initially create the file STUDY,
Art, CEO, AnalystFinder.com
Please post code and logs into code boxes from using the {i} icon.
Some of us really don't like to open Microsoft office documents because of security concerns and some are blocked by security settings. Also you can get artifacts such as the curly left/right quote vs the single quote in code and some other format changes might happen.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.