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.
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;
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.
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;
thank you
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.