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.
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
@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.
Please don't double-post questions. I have merged both posts.
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'
Can you explain in general what you expect to have as output given any input ?
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.