Fmonkey2001
Fmonkey2001

Reputation: 147

Picking rows in SAS based on a specific value in a specific column

So I'm working with a data set that has millions of rows. I'm trying to cut down the number of rows, so that I can merge this data set and another data set by zipcode.

What I'm trying to do is take a specific column "X6" and search through it for the value of "357". Then every row that has that value I want to move into a new data set.

I'm assuming that I'm going to have to use some form of if/then statement, but I can't get anything to work successfully. If needed I can post a snapshot of some of my data or what SAS code I currently have. I've seen other things that are similar, but none of them involve SAS.

Thanks for all of your help in advanced.

Upvotes: 0

Views: 1156

Answers (3)

akaDrHouse
akaDrHouse

Reputation: 2240

RamB gave a great way to parse into two datasets. If you just want a new dataset that is a subset of the original, the following will work well

 DATA NEW;
      SET ORIGINAL;
      IF X6="357";   *NOTE: THIS ASSUMES X6 IS DEFINED AS CHARACTER*
 RUN;

A nice function can also parse multiple criteria. Say you wanted to keep records where X6 = 357 or 588.

 DATA NEW;
      SET ORIGINAL;
      IF X6 IN("357","588");   *NOTE: THIS ASSUMES X6 IS DEFINED AS CHARACTER*
 RUN;

Lastly, the NOTIN also works to exclude.

Upvotes: 3

SMW
SMW

Reputation: 502

Just use Proc SQL to create your data set, then reference the value your looking for in your query -

Proc SQL;
Create table new as
Select *
From dataset 
Where x6 = 357
;
Quit;

Assuming your x6 variable is numeric...

On a mobile device...sorry for no code text

Upvotes: 0

RamB
RamB

Reputation: 428

With data step this is really simple. I'll give you an example.

data dataset_with_357
     original_without_357;
set original_dataset;

if compress(x6) = "357" then output dataset_with_357;
else output original_without_357;
run;

As I said, there are several ways of doing this, and it wasn't clear for me which is better for you.

Upvotes: 2

Related Questions