<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Extract a portion of a string variable from column name in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Extract-a-portion-of-a-string-variable-from-column-name/m-p/422305#M103837</link>
    <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have managed to get it to columns, so I'm looking to get Name1 and Name2 from Name like the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Name&lt;/TD&gt;&lt;TD&gt;Name1&lt;/TD&gt;&lt;TD&gt;Name2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;TR_ABC_S1_ABC_MNO_S3&lt;/TD&gt;&lt;TD&gt;ABC_S1&lt;/TD&gt;&lt;TD&gt;MNO_S3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;TR_ABC_S2_ABC_MNO_S3&lt;/TD&gt;&lt;TD&gt;ABC_S2&lt;/TD&gt;&lt;TD&gt;MNO_S3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;TR_ABC_S10_ABC_S2&lt;/TD&gt;&lt;TD&gt;ABC_S10&lt;/TD&gt;&lt;TD&gt;ABC_S2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;TR_ABC_S2_ABC_S10&lt;/TD&gt;&lt;TD&gt;ABC_S2&lt;/TD&gt;&lt;TD&gt;ABC_S10&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;TR_ABC_S2_ABC_PQR_S3&lt;/TD&gt;&lt;TD&gt;ABC_S2&lt;/TD&gt;&lt;TD&gt;ABC_PQR_S3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;TR_XYZ_PQR_S1_XYZ_MNO_S3&lt;/TD&gt;&lt;TD&gt;XYZ_PQR_S1&lt;/TD&gt;&lt;TD&gt;XYZ_MNO_S3&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
    <pubDate>Tue, 19 Dec 2017 15:12:03 GMT</pubDate>
    <dc:creator>ss59</dc:creator>
    <dc:date>2017-12-19T15:12:03Z</dc:date>
    <item>
      <title>Extract a portion of a string variable from column name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-a-portion-of-a-string-variable-from-column-name/m-p/422293#M103834</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My column names of my data looks like following:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;TR_ABC_S1_ABC_MNO_S3&lt;/TD&gt;&lt;TD&gt;TR_ABC_S2_ABC_MNO_S3&lt;/TD&gt;&lt;TD&gt;TR_ABC_S10_ABC_S2&lt;/TD&gt;&lt;TD&gt;TR_ABC_S2_ABC_S10&lt;/TD&gt;&lt;TD&gt;TR_ABC_S2_ABC_PQR_S3&lt;/TD&gt;&lt;TD&gt;TR_XYZ_PQR_S1_XYZ_MNO_S3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;What I intend to do is split the column name in two, splitting it after the first number. I also intend to remove the 'TR_' that it starts with. The output I'm looking for is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;TR_ABC_S1_ABC_MNO_S3&lt;/TD&gt;&lt;TD&gt;TR_ABC_S2_ABC_MNO_S3&lt;/TD&gt;&lt;TD&gt;TR_ABC_S10_ABC_S2&lt;/TD&gt;&lt;TD&gt;TR_ABC_S2_ABC_S10&lt;/TD&gt;&lt;TD&gt;TR_ABC_S2_ABC_PQR_S3&lt;/TD&gt;&lt;TD&gt;TR_XYZ_PQR_S1_XYZ_MNO_S3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ABC_S1&lt;/TD&gt;&lt;TD&gt;ABC_S2&lt;/TD&gt;&lt;TD&gt;ABC_S10&lt;/TD&gt;&lt;TD&gt;ABC_S2&lt;/TD&gt;&lt;TD&gt;ABC_S2&lt;/TD&gt;&lt;TD&gt;XYZ_PQR_S1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;MNO_S3&lt;/TD&gt;&lt;TD&gt;MNO_S3&lt;/TD&gt;&lt;TD&gt;ABC_S2&lt;/TD&gt;&lt;TD&gt;ABC_S10&lt;/TD&gt;&lt;TD&gt;ABC_PQR_S3&lt;/TD&gt;&lt;TD&gt;XYZ_MNO_S3&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried doing it using substr and scan for the first number in the string within it, but can't get it right. Could you please help? Thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 19 Dec 2017 14:48:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-a-portion-of-a-string-variable-from-column-name/m-p/422293#M103834</guid>
      <dc:creator>ss59</dc:creator>
      <dc:date>2017-12-19T14:48:20Z</dc:date>
    </item>
    <item>
      <title>Re: Extract a portion of a string variable from column name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-a-portion-of-a-string-variable-from-column-name/m-p/422303#M103835</link>
      <description>&lt;P&gt;Something like:&lt;/P&gt;
&lt;PRE&gt;data want (orig res j);
  set have;
  length orig res $200;
  array tr{6) tr_:;
  do i=1 to 6;
    orig=vname(tr{i});
    do j=1 to countw(orig,"_");
      res=scan(orig,i,"_");
      output;
    end;
  end;
run;
&lt;/PRE&gt;
&lt;P&gt;This will create a nice long list of original variable names, and then each part of the name split by _.&amp;nbsp; You can then cat() the ones you want back together, i.e. 2 and 3 for first part, then 5 and 6 for second.&lt;/P&gt;
&lt;P&gt;Note, if you post test data in the form of a datastep then working code can be provided.&lt;/P&gt;
&lt;P&gt;Second note, transposed is always harder to program with than normalised (going down).&amp;nbsp; Also don't put data in variable/table names, it causes issues like this.&amp;nbsp; Use a parameter/result approach in normalised structure - will make your life much easier.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Dec 2017 15:08:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-a-portion-of-a-string-variable-from-column-name/m-p/422303#M103835</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-12-19T15:08:16Z</dc:date>
    </item>
    <item>
      <title>Re: Extract a portion of a string variable from column name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-a-portion-of-a-string-variable-from-column-name/m-p/422305#M103837</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have managed to get it to columns, so I'm looking to get Name1 and Name2 from Name like the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Name&lt;/TD&gt;&lt;TD&gt;Name1&lt;/TD&gt;&lt;TD&gt;Name2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;TR_ABC_S1_ABC_MNO_S3&lt;/TD&gt;&lt;TD&gt;ABC_S1&lt;/TD&gt;&lt;TD&gt;MNO_S3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;TR_ABC_S2_ABC_MNO_S3&lt;/TD&gt;&lt;TD&gt;ABC_S2&lt;/TD&gt;&lt;TD&gt;MNO_S3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;TR_ABC_S10_ABC_S2&lt;/TD&gt;&lt;TD&gt;ABC_S10&lt;/TD&gt;&lt;TD&gt;ABC_S2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;TR_ABC_S2_ABC_S10&lt;/TD&gt;&lt;TD&gt;ABC_S2&lt;/TD&gt;&lt;TD&gt;ABC_S10&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;TR_ABC_S2_ABC_PQR_S3&lt;/TD&gt;&lt;TD&gt;ABC_S2&lt;/TD&gt;&lt;TD&gt;ABC_PQR_S3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;TR_XYZ_PQR_S1_XYZ_MNO_S3&lt;/TD&gt;&lt;TD&gt;XYZ_PQR_S1&lt;/TD&gt;&lt;TD&gt;XYZ_MNO_S3&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Tue, 19 Dec 2017 15:12:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-a-portion-of-a-string-variable-from-column-name/m-p/422305#M103837</guid>
      <dc:creator>ss59</dc:creator>
      <dc:date>2017-12-19T15:12:03Z</dc:date>
    </item>
    <item>
      <title>Re: Extract a portion of a string variable from column name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-a-portion-of-a-string-variable-from-column-name/m-p/422307#M103838</link>
      <description>&lt;P&gt;Then:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  length name1 name2 $200;
  name1=catx("_",scan(name,2,"_"),scan(name,3,"_"));
  name2=catx("_",scan(name,5,"_"),scan(name,6,"_"));
run;
&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Dec 2017 15:14:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-a-portion-of-a-string-variable-from-column-name/m-p/422307#M103838</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-12-19T15:14:31Z</dc:date>
    </item>
    <item>
      <title>Re: Extract a portion of a string variable from column name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-a-portion-of-a-string-variable-from-column-name/m-p/422309#M103840</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input TR_ABC_S1_ABC_MNO_S3	TR_ABC_S2_ABC_MNO_S3	TR_ABC_S10_ABC_S2	TR_ABC_S2_ABC_S10	TR_ABC_S2_ABC_PQR_S3	TR_XYZ_PQR_S1_XYZ_MNO_S3 ;
cards;
1	1	0	2	3	1
;
run;
proc transpose data=have(obs=0) out=temp;
var _all_;
run;
data want;
 set temp;
 _name_=prxchange('s/^tr_//i',1,_name_);
pid=prxparse('/[_a-z]+\d+/i');
s=1;e=length(_name_);
call prxnext(pid,s,e,_name_,p,l);
do while(p&amp;gt;0);
  temp=prxchange('s/^_//',1,substr(_name_,p,l));
  output;
  call prxnext(pid,s,e,_name_,p,l);
end;
drop s e p l pid;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Dec 2017 15:16:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-a-portion-of-a-string-variable-from-column-name/m-p/422309#M103840</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-12-19T15:16:10Z</dc:date>
    </item>
    <item>
      <title>Re: Extract a portion of a string variable from column name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-a-portion-of-a-string-variable-from-column-name/m-p/422312#M103841</link>
      <description>&lt;P&gt;I don't think you can put fixed positions like '2', '3' etc since they are shaped differently.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Dec 2017 15:24:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-a-portion-of-a-string-variable-from-column-name/m-p/422312#M103841</guid>
      <dc:creator>ss59</dc:creator>
      <dc:date>2017-12-19T15:24:31Z</dc:date>
    </item>
    <item>
      <title>Re: Extract a portion of a string variable from column name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-a-portion-of-a-string-variable-from-column-name/m-p/422315#M103843</link>
      <description>&lt;P&gt;Then loop over them:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;&lt;BR /&gt;  array name{2} $200;&lt;BR /&gt;  in=1;&lt;BR /&gt;  do i=1 to countw(name,"_");&lt;BR /&gt;    if lengthn(compress(scan(name,i,"-")," ","d")) &amp;gt; 0 then do;&lt;BR /&gt;      name{in}=catx('_',scan(name,i-1,"_"),scan(name,i,"_"));&lt;BR /&gt;      in=in+1;&lt;BR /&gt;    end;&lt;BR /&gt;  end; 
run;&amp;nbsp;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;Note assumes maximum of 2 items with a number in,&amp;nbsp; I.e. this checks each part, if it contains a digit then cat previous and current.&amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Dec 2017 15:29:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-a-portion-of-a-string-variable-from-column-name/m-p/422315#M103843</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-12-19T15:29:36Z</dc:date>
    </item>
  </channel>
</rss>

