BookmarkSubscribeRSS Feed
Thanyada
Calcite | Level 5

data date;

input date1 date2;

datalines;

25000 25000

25010 25000

25150 25150

;

 

I would like to use proc format or another method to print If date1 is not equal to date2, print "BAD" in date2.

 

how can I do with this situation?

 

Thank you for your assistance

 

3 REPLIES 3
unison
Lapis Lazuli | Level 10

Since Date1 and Date2 are date fields (numeric) you're going to have trouble printing "BAD" within the column Date2. You might try something like proc format on the difference? Format the value of 0 as "BAD". But either way you do it you're output will need to be in a character column.

 

-unison

-unison
mkeintz
PROC Star

A format, whether from the SAS format library, or defined by a user can only be assigned to a variable, not to a combination of variables.  So you would have to make a new variable signaling whether DATE1=DATE2.  And if that is the case, you could assign that new variables a character value of 'BAD'  or 'GOOD', and thereby avoid the need for a format entirely.   I.e. making a new variable is the "another method" that I would suggest.

 

You can do this in a DATA step, with an IF test on the equality of date1 and date2, containing a THEN clause that conditionally assigns the desired value.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
ballardw
Super User

With existing numeric date values then one approach to achieve your goal is to 1) assign a "special" value to the date2 variable and 2) have a custom format to display the desired text. One way:

 

proc format library=work;
   value baddate
   .B='Bad Date'
   other = [date9.]
   ;
run;
data have;
   input date1 date2;
   if date1 ne date2 then date2=.B;
   format date1 date9.  date2 baddate.;
datalines;
25000 25000
25010 25000
25150 25150
;
proc print data=have;
run;

The .B is a "special missing" value. SAS allows 27 special missing values ._ and .A through .Z. The special missing do not get used as numeric values like normal missing but you can have different display values to display different results for special cases.

 

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 1036 views
  • 1 like
  • 4 in conversation