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

Hi y'all i hope you're doing fine,

 

I'm working on a cars database and i'm having a little hard time extract name and surname of cars, my table looks like this :

 

Brand

CITROEN C3 AIRCROSS
DS3 CROSSBACK E-TENSE
OPEL GRANDLAND X

 

Now i wanna create two variables one named Make ( for the brand name ) and the other Model (for the brand model), so far so good for the Make ( because it's the first string before the space ) so with this i've succeeded getting my Make

 

Make = scan(brand,1,' ');

Now it's a little bit harder to for the model i've had few ideas but none was effective, for my model variable i want everything after the first space, i thought first to ease the process use tranwrd function and replace all my spaces with a underscore but it replaces also the space in the end with underscore, for the substr function i need the position for each observation wich a little bit hard  since each observation is different.

 

Do you have any hint for me to make this work and all the strings after the first space please ?

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Use CALL SCAN and then SUBSTR:

data have;
input brand $50.;
datalines;
CITROEN C3 AIRCROSS
DS3 CROSSBACK E-TENSE
OPEL GRANDLAND X
;

data want;
set have;
make = scan(brand,1);
call scan(brand,1,p,l);
model = substr(brand,l+2);
drop p l;
run;

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

Use CALL SCAN and then SUBSTR:

data have;
input brand $50.;
datalines;
CITROEN C3 AIRCROSS
DS3 CROSSBACK E-TENSE
OPEL GRANDLAND X
;

data want;
set have;
make = scan(brand,1);
call scan(brand,1,p,l);
model = substr(brand,l+2);
drop p l;
run;
skavli
Calcite | Level 5
Thanks that works perfectly, i gotta go see what that call function do and remember it.
Greetings sir
andreas_lds
Jade | Level 19

Try

model = substr(brand, length(make)+2);

You could, of course, use a regular expression:

data want;
   set have;
   
   length rx 8 make model $ 50;
   retain rx;
   drop rx;
   
   if _n_ = 1 then do;
      rx = prxparse('/(\S+) (.+)/');
   end;
   
   if prxmatch(rx, brand) then do;
      make = prxposn(rx, 1, brand);
      model = prxposn(rx, 2, brand);
   end;
run;
ScottBass
Rhodochrosite | Level 12

@skavli wrote:

Hi y'all i hope you're doing fine,

 

I'm working on a cars database and i'm having a little hard time extract name and surname of cars, my table looks like this :

 

Brand

CITROEN C3 AIRCROSS
DS3 CROSSBACK E-TENSE
OPEL GRANDLAND X

 

Now i wanna create two variables one named Make ( for the brand name ) and the other Model (for the brand model), so far so good for the Make ( because it's the first string before the space ) so with this i've succeeded getting my Make

 

Make = scan(brand,1,' ');

Now it's a little bit harder to for the model i've had few ideas but none was effective, for my model variable i want everything after the first space, i thought first to ease the process use tranwrd function and replace all my spaces with a underscore but it replaces also the space in the end with underscore, for the substr function i need the position for each observation wich a little bit hard  since each observation is different.

 

Do you have any hint for me to make this work and all the strings after the first space please ?


Edit your original post, using a SAS datastep with datalines statements to create your sample dataset.  Don't paste in a screenshot since if forces *us* to create *your* sample dataset.  Post using the "Insert SAS code" icon.

 

data have;
   length somevariable $200;
   somevariable='foo    bar blah';
run;

data want1;
   set have;
   length make $100 model $100;
   pos=index(somevariable,' ');
   if pos then do;
      make=substr(somevariable,1,pos);
      model=substr(somevariable,pos+1);
      model=strip(model);
   end;
run;

data want2;
   set have;
   length make $100 model $100;
   rx=prxparse('/(.*?) (.*)/o');
   if prxmatch(rx,somevariable) then do;
      make=prxposn(rx,1,somevariable);
      model=prxposn(rx,2,somevariable);
   end;
run;

Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 5471 views
  • 1 like
  • 4 in conversation