Help using Base SAS procedures

Help SAS date code to distinguish dates and blanks

Accepted Solution Solved
Reply
Contributor
Posts: 36
Accepted Solution

Help SAS date code to distinguish dates and blanks

I have got the following table.

id           Name            date              

11            john                12/04/2014

22            Matt              

26            miac               22/01/2013

I want to create a variable, ‘indicator’, that tells me that if a date is before  01/12/2014,  0  is returned and if  the date is after  this date, 1 is returned and if the date is blank 0 is returned   The problem is when SAS sees a blank  in my dataset it assumes it as a date and returns results as if the date is before my cut-off date, how do i tell sas to correctly read a blank and return 0? MY data shows a blank.

The programme I used is like this:

Length indicator $15

if  date =' ' then  indicator ='0';

if date < '15May2014'd then indicator ='1';

else date ='0';

So I want my results to look like this.

  Id           Name            date                         indicator       

11            john                12/04/2014               1

22            Matt                                                 0

26            miac               22/01/2013                0


Accepted Solutions
Solution
‎09-29-2014 02:05 PM
Contributor
Posts: 28

Re: Help SAS date code to distinguish dates and blanks

data a;

infile datalines missover;

informat date ddmmyy10.;

input Id name$ date;

  datalines;

11 john 12/04/2014

22 Matt

26 miac 22/01/2013

;run;

data a;

set a;

if date  ne . or ' ' and date lt '15May2014'd then indicator =1;

else indicator=0;

run;

View solution in original post


All Replies
Respected Advisor
Posts: 4,927

Re: Help SAS date code to distinguish dates and blanks

if missing(date) or date >= '15MAY2014'd then indicator = '0';

else indicator = '1';

PG
Super User
Posts: 19,822

Re: Help SAS date code to distinguish dates and blanks

You could reorder your code for one:

if date < '15May2014'd then indicator ='1';

else date ='0';

if  date =. then  indicator ='0';


OR change to the following:


if date < '15May2014'd and not missing(date) then indicator ='1';

else date ='0';

Super User
Super User
Posts: 7,060

Re: Help SAS date code to distinguish dates and blanks

Unlike some systems that use tri-level logic when confronted with missing (or NULL) values, SAS has decided that missing values are smaller than any valid number.  So include a test for missing in your code.

if . < date < '15May2014'd then indicator ='1';

else date ='0';

Solution
‎09-29-2014 02:05 PM
Contributor
Posts: 28

Re: Help SAS date code to distinguish dates and blanks

data a;

infile datalines missover;

informat date ddmmyy10.;

input Id name$ date;

  datalines;

11 john 12/04/2014

22 Matt

26 miac 22/01/2013

;run;

data a;

set a;

if date  ne . or ' ' and date lt '15May2014'd then indicator =1;

else indicator=0;

run;

Contributor
Posts: 23

Re: Help SAS date code to distinguish dates and blanks

Posted in reply to LearnByMistk

You're reading your date into a SAS date value which is always numeric (days since 1/1/1960). You don't need to test for ' ', because this is a character string. You'll see a log message that says

"NOTE: Character values have been converted to numeric values at the places given by:

      (Line)Smiley SadColumn)." This is caused by trying to compare the value of date with a character string.

Super Contributor
Posts: 308

Re: Help SAS date code to distinguish dates and blanks

Hello,

A proc sql solution:

data have;
infile datalines truncover;
input id Name $ date ddmmyy10.;
format date ddmmyy10.;
datalines;
11 john 12/04/2014
22 Matt
26 miac 22/01/2013
;

proc sql;
create table want as
select *, case
when date is missing or date ge  '15May2014'd   then 0
else 1
end as indicator
from have;
quit;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 3549 views
  • 2 likes
  • 7 in conversation