DATA Step, Macro, Functions and more

Retrieving observations with specific letter/number in specific position

Accepted Solution Solved
Reply
Regular Contributor
Posts: 215
Accepted Solution

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
Respected Advisor
Posts: 4,651

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

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

PG

View solution in original post


All Replies
Super User
Posts: 17,840

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

Look at the SUBSTR function. 

Solution
‎04-17-2016 12:35 PM
Respected Advisor
Posts: 4,651

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

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

PG
Regular Contributor
Posts: 215

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: 17,840

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.

Respected Advisor
Posts: 4,651

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

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

PG
☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 302 views
  • 5 likes
  • 3 in conversation