Hi,
I have a variable called MH in a dataset. I would like to extract the names (i.e., aaa or bbb...) and put them in a new variable called NEW. Thanks in advance.
obs MH
1 Seq:1; Name:aaa; Date:xxx; Reason:xxx
2 Seq:1; Name:aaaa; Date:xxx; Reason:xxx Seq:2; Name:bbb; Date:xxx; Reason:xxx
3 Seq:1; Name:aaa; Date:xxx; Reason:xxx Seq:2; Name:bb; Date:xxx; Reason:xxx Seq:3; Name:cc; Date:xxx; Reason:xxx
obs NEW
1 aaa
2 aaaa;bbb
3 aaa;bb;cc
data have;
input MH :$300.;
infile datalines dlm = '|';
datalines4;
Seq:1; Name:aaa; Date:xxx; Reason:xxx
Seq:1; Name:aaaa; Date:xxx; Reason:xxx Seq:2; Name:bbb; Date:xxx; Reason:xxx
Seq:1; Name:aaa; Date:xxx; Reason:xxx Seq:2; Name:bb; Date:xxx; Reason:xxx Seq:3; Name:cc; Date:xxx; Reason:xxx
;;;;
data temp;
set have;
group + 1;
do i = 1 to countw(MH, ';');
w = scan(MH, i, ';');
output;
end;
run;
data want;
set temp;
by group;
length new $200;
if first.group then new = '';
if find(w, 'name', 'i') then new = catx('; ', new, tranwrd(w, 'Name:', ''));
if last.group;
retain new;
run;
You could use a loop with countw and scan, untested code:
do i = 1 to countw(mh, ';');
pair = scan(mh, i, ';');
key = scan(pair, 1, ':');
if key = 'Name' then do;
new = catx(';', new, scan(pair, 2, ':');
end;
end;
Using a regex (with a loop) is another way so solve the problem.
data have;
input MH :$300.;
infile datalines dlm = '|';
datalines4;
Seq:1; Name:aaa; Date:xxx; Reason:xxx
Seq:1; Name:aaaa; Date:xxx; Reason:xxx Seq:2; Name:bbb; Date:xxx; Reason:xxx
Seq:1; Name:aaa; Date:xxx; Reason:xxx Seq:2; Name:bb; Date:xxx; Reason:xxx Seq:3; Name:cc; Date:xxx; Reason:xxx
;;;;
data temp;
set have;
group + 1;
do i = 1 to countw(MH, ';');
w = scan(MH, i, ';');
output;
end;
run;
data want;
set temp;
by group;
length new $200;
if first.group then new = '';
if find(w, 'name', 'i') then new = catx('; ', new, tranwrd(w, 'Name:', ''));
if last.group;
retain new;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.