BookmarkSubscribeRSS Feed
Fluorite | Level 6

I have a data set currently i am working on.. 


I have found this community very helpful in solving my problems. Hnece, I am posting another question. 


I have various columns in this data set. One of the columns is HospitalDay and the other Is MethadoneStartDate. 

Both these columns have numbers in them ranging from -10 to 100.. 


My question si I want to create a  new column called Methadone. The new column should have a 1 IF the value in Methadonestart date <HospitalDay. However, 

I am having a hard time creating an If then statement for this . I think the issue is the hospitalday column has every row filled with a digit, but the methadone start date has a bunch of empty rows with Dots (.) as not everyone got methadone .


How can i solve this? 

Onyx | Level 15

A dot(.) in numeric variable in SAS means "missing value", e.g.

data test;
  a = 17;
  b = 42;
  c = .;

this will create a data set Test with 3 variables, a, b, and c, but c will have missing value.


In SAS, in comparison expressions a missing value is considered to be "less than any number", so in the IF expressions like:

  if c<a then ...
  if c<b then ..

both will be evaluated as true.


So if you have data like this:

data have;
  input HospitalDay MethadoneStartDate;
20 50
50 20
20 .

the last observation in the code like this:

data want;
  set have;
  if MethadoneStartDate < HospitalDay then Methadone = 1;
                                      else Methadone = 0;

will be evaluated to true and Methadone will be set to 1.


You need to add some additional test MethadoneStartDate to be not missing, e.g.;

data want;
  set have;
  if . < MethadoneStartDate < HospitalDay then Methadone = 1;
                                          else Methadone = 0;

In this case condition says: MethadoneStartDate must be greater than missing value (which means to be non missing) and smaller than HospitalDay.


Hope it helps.









Polish SAS Users Group: and

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1 reply
  • 1 like
  • 2 in conversation