BookmarkSubscribeRSS Feed
My_SAS
Calcite | Level 5

data l;
input id $ 1-20;
cards;
aaaa1234a
bbbb1234b
ccccx1234c
asdf1234b
run;

I dont want to do by substr(1,1)=substr(2,1)


i want the output if the first character and last are same

output :

ccccx1234c
asdf1234b

9 REPLIES 9
Haikuo
Onyx | Level 15

Your output example seems inconsistent with your description, I will just go with the latter:

data l;

input id $ 1-20;

cards;

aaaa1234a

bbbb1234b

ccccx1234c

asdf1234b

run;

data want;

set l;

if first(id)=:left(reverse(id));

run;

proc print;run;

Haikuo

MikeZdeb
Rhodochrosite | Level 12

hi ... another idea ...

data want;

set l;

where char(id,1) eq char(right(id),20);

run;

Ksharp
Super User

OR You like Perl Regular Expression :

data l;
input id $ 1-20;
cards;
aaaa1234a
bbbb1234b
ccccx1234c
asdf1234b
;
run;

 
data want;
set l;
where id=prxchange('s/(\S)(.*)(\S)/\3\2\1/o',-1,id);
run;

Ksharp

My_SAS
Calcite | Level 5

Sory i have not framed my question properly

this is the req below

data l;
input id $ 1-20;
cards;
aaaa1234a
bbbb1234b
ccccx1234c
asdf1234b
run;

I dont want to do by substr(1,1)=substr(2,1)


i want the output if the first,second,third,foruth,fifth and last character and are not same.

output :

ccccx1234c
asdf1234b

Ksharp
Super User

My_SAS wrote:


i want the output if the first,second,third,foruth,fifth and last character and are same.

output :

ccccx1234c
asdf1234b

But in the case of 'ccccx1234c"

the first, -> c

second,-> c

third,-> c

foruth-> c

,fifth-> x

last character-> c

They are different . Any other logic are you using ?

Ksharp

My_SAS
Calcite | Level 5

i want the output if the first,second,third,foruth,fifth and last character and are NOT same. 

output :

ccccx1234c
asdf1234b

Ksharp
Super User

Sorry . overlook "NOT"

Oh, I undertand what you mean now.

Ksharp

My_SAS
Calcite | Level 5

should check only the characters are same not the numerics

bbbbb1234b

bbbbb      b (all the b characters are same in 1,2,3,4,5 and last obs are same)

Ksharp
Super User

How about:

data l;
input id $ 1-20;
cards;
aaaa1234a
bbbb1234b
ccccx1234c
asdf1234b
;
run;

 
data want(drop=x);
set l;
x=substrn(left(id),1,1);
if not missing (compress(id,x,'d')) ;
run;

Ksharp

Message was edited by: xia keshan

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 1156 views
  • 0 likes
  • 4 in conversation