0
votes

I created two data sets by themselves in two separate steps but now I am wondering how I could create the two new data sets in a single step in SAS

data purchase_price_jjohns2;
merge hw06.inventory hw06.purchase;
by Model;
if Quantity NE '';
TotalCost = Quantity*Price;
format TotalCost dollar7.2;
run;


data not_purchase_jjohns2;
merge hw06.inventory hw06.purchase;
by Model;
if Quantity='';
run;

my two steps by themselves now I want to know how to create this in one data step

1
Show us what you have so far, the desired end-point, and what you've tried. Edit your question to include this. - sorak
data purchase_price_jjohns2; merge hw06.inventory hw06.purchase; by Model; if Quantity NE ''; TotalCost = Quantity*Price; format TotalCost dollar7.2; run; - Jace
Put additional information in the question, not as a comment. Click 'edit' - sorak

1 Answers

0
votes

A single data step can create as many output data sets as you want. It is important to remember all output data sets are created, and the columns of the output are fixed at run-time. You can specify which variables of the pdv should be in each output data set by using the data set option (keep=...) or (drop=...). In a single data step you can not create an output data set whose name is based on some variables value -- you can preprocess the data if you need such a splitting. There are some trickeries that involve dynamic hashes, but that is an advanced topic

The if statements you are currently using are sub-setting ifs. This means output (an implicit output) only occurs when the data step flow reaches the bottom of the step. You will want an explicit OUTPUT statement to ensure the current row goes into the desired output data set.

Thus you could have a data step similar to

data want1 want2(drop=total);
  merge one two;
  by key;

  if missing(quantity) then
    OUTPUT want2;
  else do;
    total = quantity * price;
    OUTPUT want1;
  end;
run;