BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
imdickson
Quartz | Level 8

Hi everyone. I have a column that contains below possible value

132/11

132/33

33/11

11/

33/

33

11

 

I want to do the following

if whatever behind / is not blank, take the value after /

If value after / is blank, take the front

if no /, take whatever it has.

 

I tried substr(colA,1,length(colA)-1) and it wont work.

 

Can anyone help me on this?

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
input h $;
cards;
132/11
132/33
33/11
11/
33/
33
11
;
run;
data want;
 set have;
 want=scan(h,-1,'/ ');
run;

View solution in original post

8 REPLIES 8
andreas_lds
Jade | Level 19

Posting data in usable form helps us to help you.

 

i would use a regular expression: "/(.*\/)?(.+)/" and the function prxposn to get the second capture group.

DanielLangley
Quartz | Level 8

@andreas_ldsThat looks very nearly perfect. I would suggest the expression "/(.*\/)?([0-9]+)/" instead to avoid capturing the end slash if there is no data  after the slash (capture 11 instead of 11/).

132/11
132/33
33/11
11/
33/
33
11

 

@imdicksonI would suggest a website like https://regex101.com/ to test and learn regular expressions.

Shmuel
Garnet | Level 18

Alternatively use next code:

ix = index(var,'/');

if ix > 0 and ix < length(trim(var)) 
    then new_var = substr(var,ix+1);
    else new_var=compress(var,'/');

 

 

FreelanceReinh
Jade | Level 19

Hi @imdickson,

 

The combination of COALESCEC and SCAN functions can do this as well:

data have;
input h $;
cards;
132/11
132/33
33/11
11/
33/
33
11
;

data want;
set have;
length w $8;
w=coalescec(scan(h,2,'/'),scan(h,1,'/'));
run;
Kurt_Bremser
Super User

Using the "standard" string handling functions:

data have;
input h $;
cards;
132/11
132/33
33/11
11/
33/
33
11
;
run;

data want;
set have;
if countw(h,'/') <= 1
then w = h;
else do;
  w = scan(h,2,'/');
  if w = ' ' then w = scan(h,1,'/');
end;
run;

proc print data=want noobs;
run;

Result:

h         w

132/11    11
132/33    33
33/11     11
11/       11
33/       33
33        33
11        11
Ksharp
Super User
data have;
input h $;
cards;
132/11
132/33
33/11
11/
33/
33
11
;
run;
data want;
 set have;
 want=scan(h,-1,'/ ');
run;
FreelanceReinh
Jade | Level 19

@Ksharp: Elegant! (As long as no embedded blanks break it.)

data have;
input h $12.;
cards;
132/break it
;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 8 replies
  • 1208 views
  • 7 likes
  • 7 in conversation