DATA Step, Macro, Functions and more

concatenating and subsetting data sets

Accepted Solution Solved
Reply
Regular Contributor
Posts: 234
Accepted Solution

concatenating and subsetting data sets

data a;
input x y;
cards;
1 3
4 5
6 7
;
data b;
input x y;
cards;
1 4
4 7
8 9
;

data test;
   set a b;
   if x=1 then delete;
run;

I have a test code above. I want to know the process flow of this program. Does if statment acts separately on data sets i.e. read observation from a  and apply if stament on each observation and after finishing on a starts on b?  


Accepted Solutions
Solution
‎12-17-2015 07:41 PM
Super User
Posts: 10,497

Re: concatenating and subsetting data sets

Test it by adding N=_n_; after the SET but before the if.

 

And temporary variables are just that, they would not be in the output, just like _n_ isn't unless assigned to another variable that isn't dropped from the data.

View solution in original post


All Replies
Super User
Posts: 10,497

Re: concatenating and subsetting data sets

Unless you specifically tell it to examine values from only one of the data sets then any record with a value of 1 for x will be deleted.

You can use the IN dataset option to identify which dataset contributed the observation if that is needed. The IN option creates temporary variables that can be used for testing membership. The example below says to delete only if the record came from dataset b. The IN for dataset A would be completely optional for this code but provided as an example. If you are MERGEing datasets you can use code like If InA and InB ... to see if the record has contributions from both (or more than two) datasets.

data test;
   set a (in= inA)
       b (in= inB);
   if inB AND x=1 then delete;
run;

 

Regular Contributor
Posts: 234

Re: concatenating and subsetting data sets

Thanks, ballardw.  The code posted above gives me desired result (it deletes from both data sets).  I don't want to create temporary variale now (just for learning purpose) . I wanted to know how the data step worked.  Since set statment in my example stacks a and b together sequentially, does "if " statement operate on each data sets sequentially.  Basically, I want to see what happned during _n_=1? 

Solution
‎12-17-2015 07:41 PM
Super User
Posts: 10,497

Re: concatenating and subsetting data sets

Test it by adding N=_n_; after the SET but before the if.

 

And temporary variables are just that, they would not be in the output, just like _n_ isn't unless assigned to another variable that isn't dropped from the data.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 190 views
  • 1 like
  • 2 in conversation