DATA Step, Macro, Functions and more

Using an if statement with an AND

Reply
Contributor
Posts: 25

Using an if statement with an AND

Total_DOB = input(catx("/", month_dob, day_dob, year_dob),?? mmddyy8.);
if ((month_dob = 29) AND (day_dob= 02)) then Total_DOB = mdy(03,01,year_dob)-1;
else Total_DOB = mdy(month_dob,15,year_dob);

 

or

 

Total_DOB = input(catx("/", month_dob, day_dob, year_dob),?? mmddyy8.);
if ((Total_DOB = .) AND (month_dob = 29)) then Total_DOB = mdy(03,01,year_dob)-1; 
else Total_DOB = mdy(month_dob,15,year_dob);
 
neither work and just change all of the dates with a . to the day with 15
Trusted Advisor
Posts: 1,137

Re: Using an if statement with an AND

I would try the below approach if there are only two conditions

 

1) if the month_dob=02 and day_dob=29 then you want it to be imputed to 01MarYYYY. imputing only the day and month. if it is the case then what you mentioned is incorrect, you codes month_dob=29 which should be 02 as i mentioned so you reversed it.

 

2) if the above condition is not met then  you would like to impute only the day to 15.

 

Please let me know if these are only two conditions you are checking.

 

 

Thanks,
Jag
Super User
Posts: 5,513

Re: Using an if statement with an AND

Let's start with the most obvious issues and clear them up.  There might be more, but start with ...

 

Is YEAR_DOB really a two-digit year?  That would be the only reason to use ?? MMDDYY8.  But if YEAR_DOB is really a four-digit year, MMDDYY8 isn't long enough.  You need to switch to MMDDYY10. because you have to count the slashes as part of the characters you are reading.

 

It looks like you have switched around the month and the day.  It is highly unlikely that you will ever find MONTH_DOB=29.

Respected Advisor
Posts: 4,927

Re: Using an if statement with an AND

Did you know that function MDY can handle two digits year values? If the problem is that you are sometimes missing the day value, then all you need is:

 

Total_DOB = mdy (month_dob, coalesce(day_dob, 15), year_dob);

format total_DOB yymmdd10.;

PG
Super User
Posts: 7,831

Re: Using an if statement with an AND

First of all, are month_dob, day_dob, year_dob numeric or character? In your code snippet you treat them as both, which is at least not sound programming practice.

Assuming that they are numeric, I'd do

Total_DOB = input(put(year_dob,z4.) !! put(month_dob,z2.) !! put(day_dob,z2.),yymmdd8.);
if Total_DOB = .
then do;
  if ((month_dob = 29) and (day_dob = 2))
  then Total_DOB = mdy(3,1,year_dob)-1;
  else if day_dob = .
  then Total_DOB = mdy(month_dob,15,year_dob);
end;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 4 replies
  • 167 views
  • 1 like
  • 5 in conversation