data test;
infile datalines;
input jobcod $1-40;
datalines;
biostats
computer eng and architect
scientist and actor
;
run;
proc print data=test;
run;
i have created above dataset.
and i want to create two variable using above dataset data.
first variable should contain the data before "and"
second variable should contains the data after "and".
can anyone suggest the code for that?
"AND" to "TAB" for safe bet and scan
data test;
infile datalines;
input jobcod $1-40;
datalines;
biostats
computer eng and architect
scientist and actor
;
run;
data want;
set test;
w=tranwrd(jobcod,'and','09'x);
before=scan(w,1,'09'x);
after=scan(w,2,'09'x);
drop w;
run;
data want;
set test;
if indexw(jobcod, 'and') then do;
before=substr(jobcod, 1, indexw(jobcod, 'and')-1);
after=substr(jobcod, indexw(jobcod, 'and') +4);
end;
run;
proc print data=want;
run;
Please try, hope this is what you are expecting
data test;
infile datalines;
input jobcod $1-40;
if indexw(jobcod,'and')>0 then before=substr(jobcod,1,indexw(jobcod,'and')-1);
if indexw(jobcod,'and')>0 then after=substr(jobcod,indexw(jobcod,'and')+3);
datalines;
biostats
computer eng and architect
scientist and actor
;
run;
Alternatively with perl regular expression using the prxmatch and prxchange functions
data test;
infile datalines;
input jobcod $1-40;
if prxmatch('m/\band\b/',jobcod) then do;
before=prxchange('s/(.*)(and.*)/$1/',-1,jobcod);
after=prxchange('s/(.*and)(.*)/$2/',-1,jobcod);
end;
datalines;
biostats
computer eng and architect
scientist and actor
;
run;
"AND" to "TAB" for safe bet and scan
data test;
infile datalines;
input jobcod $1-40;
datalines;
biostats
computer eng and architect
scientist and actor
;
run;
data want;
set test;
w=tranwrd(jobcod,'and','09'x);
before=scan(w,1,'09'x);
after=scan(w,2,'09'x);
drop w;
run;
Thank you so much..
got what i want.
data test;
infile datalines;
input jobcod $1-40;
p=prxmatch('m/\band\b/',jobcod);
if p then do;
before=substr(jobcod,1,p-1);
after=substr(jobcod,p+4);
end;
datalines;
biostats
computer eng and architect
scientist and actor
;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.