BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
piao
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20
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;

View solution in original post

2 REPLIES 2
andreas_lds
Jade | Level 19

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.

PeterClemmensen
Tourmaline | Level 20
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;
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
  • 2 replies
  • 1669 views
  • 0 likes
  • 3 in conversation