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

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