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