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

 I have data like this.

 

 

 

Data ds;
Infile datalines;
Input idno name&$30. Team $ strtwght endwght ;
Datalines;
1331 Jason Schock paul Long blue 187 172
1067 Kanoko john rav Nagasaka green 135 122
1251 Richard roy granny Rose blue 181 166
1192 Charlene tin Armstrong yellow 152 139
1352 Bette Schock green 156 137
1262 Yao Chen Garg blue 196 180
1124 Adrienne Fink green 156 142
;

 

i nedd to get middle names of name variable

 

 the ouput should be like this;

 

middlenames  only

  Jason Schock paul Long ---------->Schock paul(middle name)


Run;

1 ACCEPTED SOLUTION

Accepted Solutions
KachiM
Rhodochrosite | Level 12

Will this help you?

 

data _null_;
length middle $20;
   set ds;
   count = countw(name, ' ');
   middle = ' ';
   do i = 2 to count -1;
      word = scan(name, i, ' ', 'm');
      call catx(' ', middle, word);
   end;
   put name = middle =;
run;

View solution in original post

5 REPLIES 5
AhmedAl_Attar
Rhodochrosite | Level 12

I would recommend looking into using the Regular Expression functions in SAS. Here is a paper with some examples to get you started

http://www.lexjansen.com/wuss/2004/data_warehousing/c_dwdb_taming_your_charac_p2.pdf

 

Good luck,

Ahmed

KachiM
Rhodochrosite | Level 12

Will this help you?

 

data _null_;
length middle $20;
   set ds;
   count = countw(name, ' ');
   middle = ' ';
   do i = 2 to count -1;
      word = scan(name, i, ' ', 'm');
      call catx(' ', middle, word);
   end;
   put name = middle =;
run;
KachiM
Rhodochrosite | Level 12

New Addition to the Earlier Solution

 

data _null_;
length middle $20;
   set ds;
   count = countw(name, ' ');
   middle = ' ';
   word1 = scan(name,1, ' ');
   word2 = scan(name, count, ' ');
   middle = transtrn(name, strip(word1), strip(' '));
   middle = transtrn(middle, strip(word2), strip(' '));

   put name = middle =;
run;

ballardw
Super User

If your data is regular enough

 

Middlename=scan(Name,3);

 

That pulls the third word from the name variable.

Your output example isn't really clear what your resulting data set would look like. I would recommend adding a new variable for the middle name.

You might also then consider removing the middle name from the name field but it isn't clear if that was what you intend.

Ksharp
Super User
Here are two ways . One is CALL SCAN() , another is prxchange().



Data ds;
Infile datalines;
Input idno name&$30. Team $ strtwght endwght ;
call scan(name,1,p1,l1);
call scan(name,-1,p2,l2);
Middle_Name1=strip(substr(name,l1+1,p2-l1-1));

Middle_Name2=prxchange('s/^\w+|\w+$//',-1,strip(name));

drop p1 p2 l1 l2;
Datalines;
1331 Jason Schock paul Long   blue 187 172
1067 Kanoko john rav Nagasaka   green 135 122
1251 Richard roy granny Rose   blue 181 166
1192 Charlene tin Armstrong   yellow 152 139
1352 Bette Schock   green 156 137
1262 Yao Chen Garg   blue 196 180
1124 Adrienne Fink   green 156 142
;
run;

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
  • 5 replies
  • 5064 views
  • 2 likes
  • 5 in conversation