BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mlogan
Lapis Lazuli | Level 10

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

1 ACCEPTED SOLUTION
5 REPLIES 5
mlogan
Lapis Lazuli | Level 10
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,
Reeza
Super User

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

or use Reverse function and take the first character.

PGStats
Opal | Level 21

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

PG

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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