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

 

I am trying to get this output:

 

Status Days DATE1 DATE2 flag
Unknown 20 9-Sep-18 10-Sep-18 Dates do not match
Unknown 30 . 10-Sep-18 data is missing
  40 10-Sep-18 10-Sep-18 data is missing
Unknown 40 10-Sep-18 10-Sep-18  

 

 

data have;
length Status $10 Days 8.;
input STATUS $ DATE1 :date9. DATE2 :date9. Days;
format DATE1 date9. DATE2 date9. ;
datalines;
Unknown 09Sep2018 10Sep2018 20
Unknown . 10Sep2018 30
. 10Sep2018 10Sep2018 40
Unknown 10Sep2018 10Sep2018 40
;
run;

 

data want;
set have;
length flag $ 60;
if DATE1 ne null and DATE2 ne null and DATE1 ne DATE2 then do flag = "Dates do not match"; end;
if (DATE1 =null or Status = null) and (Days > 20) then do flag="data is missing"; end;
run;

 

 

The first if statement works, but the second does not.  Any help would be truly appreciated.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

The first statement working is a happy accident.  It forces SAS to create a variable named NULL which will be numeric and contain a missing value.  So you accidentally end up checking whether DATE1 contains a missing value, and whether DATE2 contains a missing value.

 

NULL is not a keyword in a DATA step (which is why your second statement runs into trouble).  Try it this way:

 

data want;
set have;
length flag $ 60;
if (DATE1 ne .) and (DATE2 ne .) and DATE1 ne DATE2 then flag = "Dates do not match"; 
if (DATE1 = . or Status = " ") and (Days > 20) then flag="data is missing"; 
run;

View solution in original post

4 REPLIES 4
Reeza
Super User

DATE1 =null this may be your issue, is there a note in your log to that effect?

I get one when I try and use NULL.

 

I think the valid syntax in SAS would be using the MISSING. I recommend missing because it works for numeric and character. Otherwise a numeric missing is denoted with a period and a character missing is with a space.

 

if not missing(date1)  then ...;

*check how many missing among a list of variables;
if nmiss(date1, date2) eq 0 then ...;

*if numeric variable;
if date1 ne . then ...;

*if characater variable;
if date1 ne '' then ...;

@arde wrote:

 

I am trying to get this output:

 

Status Days DATE1 DATE2 flag
Unknown 20 9-Sep-18 10-Sep-18 Dates do not match
Unknown 30 . 10-Sep-18 data is missing
  40 10-Sep-18 10-Sep-18 data is missing
Unknown 40 10-Sep-18 10-Sep-18  

 

 

data have;
length Status $10 Days 8.;
input STATUS $ DATE1 :date9. DATE2 :date9. Days;
format DATE1 date9. DATE2 date9. ;
datalines;
Unknown 09Sep2018 10Sep2018 20
Unknown . 10Sep2018 30
. 10Sep2018 10Sep2018 40
Unknown 10Sep2018 10Sep2018 40
;
run;

 

data want;
set have;
length flag $ 60;
if DATE1 ne null and DATE2 ne null and DATE1 ne DATE2 then do flag = "Dates do not match"; end;
if (DATE1 =null or Status = null) and (Days > 20) then do flag="data is missing"; end;
run;

 

 

The first if statement works, but the second does not.  Any help would be truly appreciated.

 

 


 

arde
Obsidian | Level 7
thank you
Astounding
PROC Star

The first statement working is a happy accident.  It forces SAS to create a variable named NULL which will be numeric and contain a missing value.  So you accidentally end up checking whether DATE1 contains a missing value, and whether DATE2 contains a missing value.

 

NULL is not a keyword in a DATA step (which is why your second statement runs into trouble).  Try it this way:

 

data want;
set have;
length flag $ 60;
if (DATE1 ne .) and (DATE2 ne .) and DATE1 ne DATE2 then flag = "Dates do not match"; 
if (DATE1 = . or Status = " ") and (Days > 20) then flag="data is missing"; 
run;

arde
Obsidian | Level 7

thank you

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 897 views
  • 0 likes
  • 3 in conversation