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

I am trying to extract the middle 2 numbers from the group names below: 

 

  • VIPER170891
  • HEAT170992
  • BULLS170691

 

I applied the formula below; however, I am getting an error. 

 

SUBSTR(RIGHT(SCAN(t1.GROUPNAME, -1, ' ')),4,2)
 
Can someone help ? 
 
Thanks, 
 
Chris

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Here is one way:

data have;
  informat groupname $15.;
  input groupname;
  want=substr(groupname,anydigit(groupname)+2,2);
  cards;
VIPER170891
HEAT170992
BULLS170691
;

Art, CEO, AnalystFinder.com

 

View solution in original post

5 REPLIES 5
art297
Opal | Level 21

Here is one way:

data have;
  informat groupname $15.;
  input groupname;
  want=substr(groupname,anydigit(groupname)+2,2);
  cards;
VIPER170891
HEAT170992
BULLS170691
;

Art, CEO, AnalystFinder.com

 

chrisjab
Fluorite | Level 6

Thank you for your help; It worked ! 

art297
Opal | Level 21

After posting my suggestion, I noticed that your initial code looked like it was pulled from a proc sql instance. If so, the following would work:

data have;
  informat groupname $15.;
  input groupname;
  cards;
VIPER170891
HEAT170992
BULLS170691
;

proc sql;
  create table want as
    select substr(t1.groupname,anydigit(groupname)+2,2) as want
      from have t1
  ;
quit;

Art, CEO, AnalystFinder.com

 

novinosrin
Tourmaline | Level 20

if your pattern holds true for all obs

 

data have;
  informat groupname $15.;
  input groupname;
  want=reverse(substr(reverse(strip(groupname)),3,2));
  cards;
VIPER170891
HEAT170992
BULLS170691
;


/*if reading a dataset*/
data have;
 informat groupname $15.;
 input groupname;
 cards;
VIPER170891
HEAT170992
BULLS170691
;

data want;
set have;
want=reverse(substr(reverse(strip(groupname)),3,2));
run;

 

novinosrin
Tourmaline | Level 20

Another way, this 'kd' thing was shared by Art to me only yesterday 🙂

 

data have;
  informat groupname $15.;
  input groupname;
  cards;
VIPER170891
HEAT170992
BULLS170691
;

data want;
set have;
want=substr(compress(strip(groupname),'','kd'),3,2) ;
run;

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 5 replies
  • 1141 views
  • 0 likes
  • 3 in conversation