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
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;
if missing(date) or date >= '15MAY2014'd then indicator = '0';
else indicator = '1';
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';
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';
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;
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.
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.