DATA Step, Macro, Functions and more

Subsetting by if

Accepted Solution Solved
Reply
Regular Contributor
Posts: 161
Accepted Solution

Subsetting by if

[ Edited ]

Hi, 

I'm trying to subset the observations that match the criteria 

by if statement but my code below  doesn't work 

I know I can do it by where statement  but I also want to do it by if 

 

data new; 
set have;
if subject_id ne "%999%";
run;

sample of the data 

 

subject_id
200018001
200018002
200018003
200018005
200019999
200019999
200028001
200028002
200028005
200029999
200059999
200079999
200089999
200089999
200098001
200098002
200098003
200098004
200099999
200108001
200108002
200108003
200108004
200109999

 

 

 

 


Accepted Solutions
Solution
‎06-06-2018 12:53 PM
Super User
Posts: 10,280

Re: subsetting by if

[ Edited ]

Use

if index(subject_id,"999") = 0;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Solution
‎06-06-2018 12:53 PM
Super User
Posts: 10,280

Re: subsetting by if

[ Edited ]

Use

if index(subject_id,"999") = 0;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 23,776

Re: Subsetting by if

% is a wildcard in the WHERE statement with the LIKE operator, not an IF statement. 

WHERE statements allow SQL functions, IF statements do not. 

 

where subject_id like '%999%';

The equiavelnt IF statement would be to use FIND() or INDEX() to search for that string. 

 

if find('999', employee_id) > 0 ;
☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 95 views
  • 2 likes
  • 3 in conversation