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

Hello All, 

 

I am trying to write a query to replace missing values for the column Shipmode and they have a correlation with the difference of ShipDate and OrderDate.  There are 4 types of ShipMode, 

 

Same day if the ship date and order date are the same, First class if its below 3 days, Second class if its below 4 days and everything else is Standard Class

 

I tried the following query with no luck. It is replacing my original dataset with only the values of those with the missing values. I am an absolute beginner to SAS, so any help on this would be deeply appreciated 

 

data apacstore;

set work.apacstore;

if shipdate-orderdate <=1 then shipmode = 'Same Day';
if shipdate-orderdate > 1 or shipdate-orderdate < 3 then shipmode = 'First Class';
if shipdate-orderdate >= 3 or shipdate-orderdate <= 4 then shipmode 'Second Class';
if shipdate-orderdate > 4 then shipmode = 'Standard Class';
where shipmode is missing;
run; 

 

ShipModeMissing.png

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Amir
PROC Star

The where statement causes the data step to only read in the records with a missing shipmode, so that is why you do not see any records with a non-missing value for shipmode.

 

An alternative is to use conditional logic, as below, with an if statement, so that all records go to the output data set. I have also updated some of the logic by using else as well.

 

data apacstore;

set work.apacstore;

   if missing(shipmode) then
   do;
      if shipdate-orderdate <=1 then
         shipmode = 'Same Day';
      else
         if shipdate-orderdate < 3 then
            shipmode = 'First Class';
         else
            if shipdate-orderdate <= 4 then
               shipmode = 'Second Class';
            else
               shipmode = 'Standard Class';
   end;
run; 

 

Kind regards,

Amir.

View solution in original post

4 REPLIES 4
Amir
PROC Star

The where statement causes the data step to only read in the records with a missing shipmode, so that is why you do not see any records with a non-missing value for shipmode.

 

An alternative is to use conditional logic, as below, with an if statement, so that all records go to the output data set. I have also updated some of the logic by using else as well.

 

data apacstore;

set work.apacstore;

   if missing(shipmode) then
   do;
      if shipdate-orderdate <=1 then
         shipmode = 'Same Day';
      else
         if shipdate-orderdate < 3 then
            shipmode = 'First Class';
         else
            if shipdate-orderdate <= 4 then
               shipmode = 'Second Class';
            else
               shipmode = 'Standard Class';
   end;
run; 

 

Kind regards,

Amir.

Kumarathevan
Fluorite | Level 6

thank you very much. I have a better understanding on how the where statement works now! 

Tom
Super User Tom
Super User

Note that placing the WHERE statement miles away from the SET statement is probably contributing to this type of confusion.  It doesn't confuse SAS, but will most certainly confuse programmers looking at the code into the false impression that the WHERE is the last thing that happens.  If you want to use WHERE on the way out of the data step use the WHERE= dataset option on the output dataset specification.

Also clear up your logic in your IF/THEN sequence..  Your existing code will only create the first two levels of SHIPMODE, since any number (including missing values) is either greater than 1 or it isn't.

data apacstore(where=(shipmode is missing;));
  set work.apacstore;
  if (shipdate-orderdate) >.Z AND (shipdate-orderdate) <=1 then shipmode = 'Same Day';
  else if (shipdate-orderdate) > 1 AND (shipdate-orderdate) < 3 then shipmode = 'First Class';
  else if (shipdate-orderdate) >= 3 AND (shipdate-orderdate) <= 4 then shipmode 'Second Class';
  else if shipdate-orderdate > 4 then shipmode = 'Standard Class';
run; 

 

Kumarathevan
Fluorite | Level 6
Thank You Tom! really appreciate your insights on these best practices. I am a total noob trying to complete an assignment here. Learning tonnes through this forum!

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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