BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
r4321
Pyrite | Level 9

Hello all, 

 

Forgive me if my code is a bit elementary, but I code use some help with a simple data step question. 

 

I am trying to keep a subset of observations in my data. I've often done this in the past by using if var = "..." then delete in a data step. However, if I did it that way, I would have far too many to delete. Alternatively, if i did it by using if var ne "..." then delete, I would have to use a bunch of data steps and then merge into a large file, which is also not super convenient. I'm wondering can I do something like

 

data want;
set have;
keep if var1 = "CharacterName";
keep if var1 = "CharacterName1";
keep if var1 = "CharacterName2";
keep if var1 = "CharacterName3";
run;

 

However, when I run this, it doesn't work of course. 

 

Any suggestions? 

 

Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@r4321 

The KEEP statement defines variables (columns) you want to keep in your output data set (the one defined using DATA ....). SAS tables are rectangular so it's either keep a column or don't. Also for this reason a KEEP statement is never conditional.

 

You want to remove rows. With SAS you normally simply create a new table (WANT in your case) and then select the rows you want.

 

Syntax you could use:

if var1 in ('CharacterName', 'CharacterName1',....);

 

View solution in original post

2 REPLIES 2
Patrick
Opal | Level 21

@r4321 

The KEEP statement defines variables (columns) you want to keep in your output data set (the one defined using DATA ....). SAS tables are rectangular so it's either keep a column or don't. Also for this reason a KEEP statement is never conditional.

 

You want to remove rows. With SAS you normally simply create a new table (WANT in your case) and then select the rows you want.

 

Syntax you could use:

if var1 in ('CharacterName', 'CharacterName1',....);

 

r4321
Pyrite | Level 9
Thank you!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 484 views
  • 1 like
  • 2 in conversation