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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

"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;

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20
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;
Jagadishkatam
Amethyst | Level 16

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;
Thanks,
Jag
Jagadishkatam
Amethyst | Level 16

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;
Thanks,
Jag
novinosrin
Tourmaline | Level 20

"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;
Soham0707
Obsidian | Level 7

Thank you so much..

got what i want.

Ksharp
Super User
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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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