BookmarkSubscribeRSS Feed
MSK4
Obsidian | Level 7

data have_1;
input company $40.;
cards;
Ind Business company
Ind school company
Ind Business school company
;
run;

data want_1;
set have_1;
new=substr(company,5,findw(company,'company')-1-4);
run;

 

I tried this i got output what i expected  but the code seems to be not perfect

output

Business

school

Business school

 

can you please correct me.

 

5 REPLIES 5
MSK4
Obsidian | Level 7

data have_2;
input company $40.;
cards;
Indian Business company
Ind school company
australia Business school company
;

i have tried but i haven't got output.i want output like this 

Business

school

Business school

Kurt_Bremser
Super User

@MSK4 wrote:

data have_2;
input company $40.;
cards;
Indian Business company
Ind school company
australia Business school company
;

i have tried but i haven't got output.i want output like this 

Business

school

Business school


Use a DO loop from 2 to COUNTW - 1, extract the words with SCAN, and concatenate with CATX.

andreas_lds
Jade | Level 19

Please don't double-post questions. I have merged both posts.

s_lassen
Meteorite | Level 14

So, basically you want to drop the first word, which is nationality, and the word "company". Here is a solution using SCAN and CATX:

 

data want;
  set have_1;
  length new $40;
  do _N_=2 to countw(company) while (upcase(scan(company,_N_)) ne 'COMPANY');
    call catx(' ',new,scan(company,_N_));
    end;
run;

Another possibility is to use Pearl regular expressions:

 

 

data want;
  set have_1;
  new=prxchange('s/^\S*\s+//',1,company);
  new=prxchange('s/\s+company\b//i',1,new);
run;

This works slightly different, as any words after "company" are retained (if any) and the SCAN solution drops them. If you want to get rid of those as well with PRX, change the last expression to 's/\s+company\b.*//i'

 

RichardDeVen
Barite | Level 11

Can you explain in general what you expect to have as output given any input ?

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
  • 5 replies
  • 4796 views
  • 0 likes
  • 5 in conversation