BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
zetter
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
LearnByMistk
Obsidian | Level 7

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

6 REPLIES 6
PGStats
Opal | Level 21

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

else indicator = '1';

PG
Reeza
Super User

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';

Tom
Super User Tom
Super User

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';

LearnByMistk
Obsidian | Level 7

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;

cov_derek
Fluorite | Level 6

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):(Column)." This is caused by trying to compare the value of date with a character string.

Loko
Barite | Level 11

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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