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

Hi everyone, 

 

I really appreciate all you help on this. I have racked my brain for hours to solve it using perl reg experation but no luck. 

 

I wish to create a variable called "name" from the variable "text" from the below dataset. 

 

 

data Have;
ID=1;
text='total16z01_model_avg_jan_2016';
output;
ID=2;
text='total17z02_model_1_avg_feb_2017';
output;
ID=3;
text='total13b1_model_2_avg_2013';
output;
run;

desired output: 

 

 

ID text name

total16z01_model_avg_jan_2016 total16z01

total17z02_model_1_avg_feb_2017 total17z02_1

total13b1_model_2_avg_2013 total13b1_2

 

Thanks for you help 

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Here is one way:

data want;
  set have;
  name=scan(text,1,'_');
  if anydigit(scan(text,3,'_')) then name=
    catx('_',name,scan(text,3,'_'));
run;

Art, CEO, AnalystFinder.com

View solution in original post

4 REPLIES 4
art297
Opal | Level 21

Here is one way:

data want;
  set have;
  name=scan(text,1,'_');
  if anydigit(scan(text,3,'_')) then name=
    catx('_',name,scan(text,3,'_'));
run;

Art, CEO, AnalystFinder.com

Astounding
PROC Star

Without knowing much about regular expressions, I would just have to solve it the old-fashioned way:

 

data want;

set have;

name = scan(text, 1, '_');

word3 = scan(text, 3, '_');

if input(word3, ??5.) > . then name = trim(name) || '_' || word3;

drop word3;

run;

slchen
Lapis Lazuli | Level 10

name=prxchange('s/_[a-z]+|_\d+$//i',-1,strip(text));

zqkal
Obsidian | Level 7

Thank you for you help. This solution also works 

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
  • 4 replies
  • 841 views
  • 2 likes
  • 4 in conversation