## Retrieving observations with specific letter/number in specific position

Solved
Regular Contributor
Posts: 244

# Retrieving observations with specific letter/number in specific position

Hi All,

I have the follwoing dataset and I want to retrieve the observations where variable CODE has 3 or 4 in its fourth position. can someone write the code for me please. Thanks.

data have;
input ID CODE\$ 7.;
cards;
101   BIO4MU
101   ENG3NO
102   PHY2L
102   LOS1KO
103   HUM3P
;
RUN

Expected Output

ID      CODE
101   BIO4MU
101   ENG3NO
103   HUM3P

Accepted Solutions
Solution
‎04-17-2016 12:35 PM
Posts: 5,521

## Re: Retrieving observations with specific letter/number in specific position

if char(code,4) in ("3","4");

PG

All Replies
Super User
Posts: 23,663

## Re: Retrieving observations with specific letter/number in specific position

Look at the SUBSTR function.

Solution
‎04-17-2016 12:35 PM
Posts: 5,521

## Re: Retrieving observations with specific letter/number in specific position

if char(code,4) in ("3","4");

PG
Regular Contributor
Posts: 244

## Re: Retrieving observations with specific letter/number in specific position

Hi PG Stats,
Would to please help me with the code to find where the observations ends with 'C' and at the same time 'C' appears in the sixth position as well. Simething like: 12ABDC, BH3GLC.

So far I have written what finds the C in the sixth position:

DATA Want;
SET Have;
IF char(var1,6) in ("C");
RUN;

Thanks,
Super User
Posts: 23,663

## Re: Retrieving observations with specific letter/number in specific position

Use length(var1) to find the length of a variable and the corresponding last character

or use Reverse function and take the first character.

Posts: 5,521

## Re: Retrieving observations with specific letter/number in specific position

IF char(var1, 6) = "C" and length(var1) = 6;

PG
🔒 This topic is solved and locked.