DATA Step, Macro, Functions and more

IF Statements Problem: How to keep more?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

IF Statements Problem: How to keep more?

data west;
set x;
IF no="04";
IF no="21";
IF no="23";
run;

 

There is a variable NO. I would like to keep no=04, no=21 and no=23 at the same time.

However, This code doesn't work.

Besides, I run it one by one and then set together. Is there any other way else to do it?


Accepted Solutions
Solution
‎11-05-2017 11:06 PM
PROC Star
Posts: 2,311

Re: IF Statements Problem: How to keep more?

Posted in reply to audreyliu201

if NO = '04';

 

is the same as

 

if NO ne '04' then delete;

 

It means: only keep if NO='04' .

 

You want 

 

if NO in ('04','21','23');

View solution in original post


All Replies
Solution
‎11-05-2017 11:06 PM
PROC Star
Posts: 2,311

Re: IF Statements Problem: How to keep more?

Posted in reply to audreyliu201

if NO = '04';

 

is the same as

 

if NO ne '04' then delete;

 

It means: only keep if NO='04' .

 

You want 

 

if NO in ('04','21','23');

Occasional Contributor
Posts: 7

Re: IF Statements Problem: How to keep more?

Thank you. This is very clean and simple 

Super User
Super User
Posts: 9,399

Re: IF Statements Problem: How to keep more?

Posted in reply to audreyliu201

You would be better off storing those numeric values as numeric variables rather than text, unless there is a really good reason not to.  For instance, calculations, where's, joins etc. all will work better with numerics, and if you say you need the 0 where only one digit is present, then use the z2. format.  Best of both worlds then.

if no in (4,21,23);
PROC Star
Posts: 2,311

Re: IF Statements Problem: How to keep more?

[ Edited ]

@RW9 Good point. Another school of thought is that numeric variables should only be for values that can be used for calculations, and other values (like IDs) should be strings. Strings do need to use exact values ( '9' is not the same as ' 9' or '09' ) but this is not necessarily detrimental. I reckon the difference is just something to be aware of, not a reason to always use one or the other.

 

As for speed, strings can be be faster when merging.

data T1 T2;  do I=1 to 1e8-1; output; end;
data _null_; 
  merge T1 T2; by I; 
run;
*NOTE: DATA statement used (Total process time):
       real time           27.15 seconds
       user cpu time       25.14 seconds
       system cpu time     2.01 seconds;


data T1 T2;  do I=1 to 1e8-1; A=put(I,z8.);output; end; drop I;
data _null_; 
  merge T1 T2; by A; 
run;
*NOTE: DATA statement used (Total process time):
       real time           25.58 seconds
       user cpu time       23.13 seconds
       system cpu time     2.44 seconds;

 

 

 

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 151 views
  • 4 likes
  • 3 in conversation