SAS: The way to Utility (in=a) in Merge Remark


When merging two datasets in SAS, you’ll importance the IN observation to just go back rows the place a price exists in a selected dataset.

Listed below are a couple of habitual tactics to importance the IN observation in apply:

Mode 1: Go back Rows the place Worth Exists in First Dataset (in = a)

knowledge final_data;
  merge data1 (in=a) data2;
  through ID;
  if a;
run;

This actual instance merges the datasets known as data1 and data2 and most effective returns the rows the place a price exists in data1.

Mode 2: Go back Rows the place Worth Exists in 2d Dataset (in = b)

knowledge final_data;
  merge data1 data2 (in=b);
  through ID;
  if b;
run;

This actual instance merges the datasets known as data1 and data2 and most effective returns the rows the place a price exists in data2.

Mode 3: Go back Rows the place Worth Exists in Each Datasets (in = a) and (in = b)

knowledge final_data;
  merge data1 (in = a) data2 (in=b);
  through ID;
  if a and b;
run;

This actual instance merges the datasets known as data1 and data2 and most effective returns the rows the place a price exists inĀ each data1 and data2.

Refer to examples display the best way to importance each and every form in apply with please see two datasets:

/*form first dataset*/
knowledge data1;
    enter ID Gender $;
    datalines;
1 Male
2 Male
3 Feminine
4 Male
5 Feminine
;
run;

identify "data1";
proc print knowledge = data1;

/*form 2d dataset*/
knowledge data2;
    enter ID Gross sales;
    datalines;
1 22
2 15
4 29
6 31
7 20
8 13
;
run;

identify "data2";
proc print knowledge = data2;

Instance 1: Go back All Rows

We will be able to importance please see merge observation with none IN observation to merge the 2 datasets in keeping with the worth within the ID column and go back all rows from each datasets:

/*carry out merge*/
knowledge final_data;
  merge data1 data2;
  through ID;
run;

/*view effects*/
identify "final_data";
proc print knowledge=final_data;

Understand that each one rows from each datasets are returned, regardless if there are lacking values because of an ID worth now not current in each datasets.

Instance 2: Go back Rows the place Worth Exists in First Dataset (in = a)

We will be able to importance please see merge observation with (in = a) to merge the 2 datasets in keeping with the worth within the ID column and go back most effective the rows the place a price exists within the first dataset:

/*carry out merge*/
knowledge final_data;
  merge data1 (in = a) data2;
  through ID;
  if a;
run;

/*view effects*/
identify "final_data";
proc print knowledge=final_data;

Understand that most effective the rows the place a price exists within the first dataset are returned.

Instance 3: Go back Rows the place Worth Exists in 2d Dataset (in = b)

We will be able to importance please see merge observation with (in = b) to merge the 2 datasets in keeping with the worth within the ID column and go back most effective the rows the place a price exists in the second one dataset:

/*carry out merge*/
knowledge final_data;
  merge data1 data2 (in = b);
  through ID;
  if b;
run;

/*view effects*/
identify "final_data";
proc print knowledge=final_data;

Understand that most effective the rows the place a price exists in the second one dataset are returned.

Instance 4: Go back Rows the place Worth Exists in Each Datasets (in = a) and (in = b)

We will be able to importance please see merge observation with (in = a) and (in = b) to merge the 2 datasets in keeping with the worth within the ID column and go back most effective the rows the place a price exists in each datasets:

/*carry out merge*/
knowledge final_data;
  merge data1 (in = a) data2 (in = b);
  through ID;
  if a and b;
run;

/*view effects*/
identify "final_data";
proc print knowledge=final_data;

Understand that most effective the rows the place a price exists in each datasets are returned.

Be aware: You’ll be able to in finding your entire documentation for the SAS merge observation right here.

Alternative Sources

Refer to tutorials give an explanation for the best way to carry out alternative habitual duties in SAS:

The way to Carry out a Left Secured in SAS
The way to Carry out an Inside Secured in SAS
The way to Carry out an Outer Secured in SAS

Leave a Comment