DATA Step, Macro, Functions and more

If-then statement and date constant

Reply
Occasional Contributor
Posts: 10

If-then statement and date constant

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

Super User
Posts: 17,744

Re: If-then statement and date constant

Do you think it might help if we saw the error message?

PROC Star
Posts: 7,356

Re: If-then statement and date constant

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

 

Super User
Posts: 17,744

Re: If-then statement and date constant

@art297 How can you see the irregular quotes in the post? They all look the same to me.

Super User
Posts: 17,744

Re: If-then statement and date constant

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';
PROC Star
Posts: 7,356

Re: If-then statement and date constant

[ Edited ]

@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

 


Capture.JPG
Occasional Contributor
Posts: 10

Re: If-then statement and date constant

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.

Occasional Contributor
Posts: 10

Re: If-then statement and date constant

Sorry I wanted to attached this file earlier but forgot to do so.

PROC Star
Posts: 7,356

Re: If-then statement and date constant

[ Edited ]

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

Super User
Posts: 10,461

Re: If-then statement and date constant

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.

Ask a Question
Discussion stats
  • 9 replies
  • 151 views
  • 1 like
  • 4 in conversation