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

Hi

 

i want to create three variable using a string 

a ='rahul99yadav'

output should be

first_name           last_name       Id

rahul                    yadav              99

 

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

Have you used regular expressions?

 

data have_want;
    length 
        first_name last_name $ 40 
        id $ 10
    ;
    
    rx = prxparse('/(\D+)(\d+)(\D+)/');
    
    a ='rahul99yadav';
    
    if prxmatch(rx, a) then do;
        first_name = prxposn(rx, 1, a);
        id = prxposn(rx, 2, a);
        last_name = prxposn(rx, 3, a);       
    end;
    
    drop rx;
run;
    

View solution in original post

6 REPLIES 6
KentaMURANAKA
Pyrite | Level 9

I do not know what structure your data have, but how about this?

data test;
    length x $200.;
    input x $;
    datalines;
    taro01yamada
    gonbe02nanashi
    ;
run;


data test_1;
    set test;


    attrib first last id length=$200.;


    v=verify(x, "abccdefghijklmnopqrstuvwxyz");


    first=substr(x, 1, verify(x, "abccdefghijklmnopqrstuvwxyz")-1);
    last=substr(substr(x, verify(x, "abccdefghijklmnopqrstuvwxyz")), verify(substr(x, verify(x, "abcdefghijklmnopqrstuvwxyz")), "0123456789"));
    id=substr(substr(x, verify(x, "abccdefghijklmnopqrstuvwxyz")), 1, verify(substr(x, verify(x, "abcdefghijklmnopqrstuvwxyz")), "0123456789")-1);
run;
japelin
Rhodochrosite | Level 12

please try this code.

 

data have;
  length a $200;
  input a;
datalines;
rahul99yadav
;
run;

data want;
  set have;
  length first_name $20 last_name $20 Id $8;
  first_name=scan(a,1,,'d');
  last_name =scan(a,2,,'d');
  Id=scan(a,1,,'dk');
run;
andreas_lds
Jade | Level 19

Have you used regular expressions?

 

data have_want;
    length 
        first_name last_name $ 40 
        id $ 10
    ;
    
    rx = prxparse('/(\D+)(\d+)(\D+)/');
    
    a ='rahul99yadav';
    
    if prxmatch(rx, a) then do;
        first_name = prxposn(rx, 1, a);
        id = prxposn(rx, 2, a);
        last_name = prxposn(rx, 3, a);       
    end;
    
    drop rx;
run;
    
aanan1417
Quartz | Level 8

Thanks a ton

Ksharp
Super User

Does it exactly have three variables ?

 

data test;
    length x $200.;
    input x $;
    datalines;
    taro01yamada
    gonbe02nanashi
    ;
run;

data want;
 set test;
 v1=scan(x,1,,'d');
 v2=scan(x,2,,'d');
 v3=scan(x,1,,'kd');
run;
aanan1417
Quartz | Level 8
Thanks a ton

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 6 replies
  • 1569 views
  • 5 likes
  • 5 in conversation