2.4 C
London
Friday, December 20, 2024
HomeSASData Munging in SASSAS: How to Use EXCEPT in PROC SQL

SAS: How to Use EXCEPT in PROC SQL

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the EXCEPT operator in the PROC SQL statement in SAS to only return rows from one dataset that are not in another dataset.

The following example shows how to use the EXCEPT operator in practice.

Example: Using EXCEPT in PROC SQL in SAS

Suppose we have the following dataset in SAS that contains information about various basketball players:

/*create first dataset*/
data data1;
    input team $ points;
    datalines;
A 12
A 14
A 15
A 18
A 20
A 22
;
run;

/*view first dataset*/
proc print data=data1;

And suppose we have another dataset in SAS that also contains information about various basketball players:

/*create second dataset*/
data data2;
    input team $ points;
    datalines;
A 12
A 14
B 23
B 25
B 29
B 30
;
run;

/*view second dataset*/
proc print data=data2;

We can use the EXCEPT operator in the PROC SQL statement to only return the rows from the first dataset that are not in the second dataset

/*only return rows from first dataset that are not in second dataset*/
proc sql;
   title 'data1 EXCEPT data2';
   select * from data1
   except
   select * from data2;
quit;

Notice that only the rows in the first dataset that do not belong to the second dataset are returned.

We can also use the EXCEPT operator to only return the rows from the second dataset that are not in the first dataset:

/*only return rows from second dataset that are not in first dataset*/
proc sql;
   title 'data2 EXCEPT data1';
   select * from data2
   except
   select * from data1;
quit;

Notice that only the rows in the second dataset that do not belong to the first dataset are returned.

Additional Resources

The following tutorials explain how to perform other common tasks in SAS:

SAS: How to Use UNION in PROC SQL
SAS: How to Use Proc Univariate by Group
SAS: How to Use Proc Contents

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories