<?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: Divide a variable into several variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Divide-a-variable-into-several-variables/m-p/399028#M96591</link>
    <description>Fredrik,&lt;BR /&gt;&lt;BR /&gt;Thank you for your help!&lt;BR /&gt;Sure, having delimiter can definitely make things easier, but sometimes things just don't go that well.</description>
    <pubDate>Wed, 27 Sep 2017 05:46:32 GMT</pubDate>
    <dc:creator>Chung-Li</dc:creator>
    <dc:date>2017-09-27T05:46:32Z</dc:date>
    <item>
      <title>Divide a variable into several variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Divide-a-variable-into-several-variables/m-p/398736#M96483</link>
      <description>&lt;P&gt;Hi there,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I was processing dataset this morning, I faced a problem.&lt;/P&gt;&lt;P&gt;Let's take a look at my data first:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;30210027甲三郎30210397乙先天冰30210069丙小姐30210493丁阿旺&lt;/P&gt;&lt;P&gt;30210044物阿梅30210021己阿有30240052更有言30210346心一?&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Basic it's in the pattern&amp;nbsp;of "8-digit number" + "Chinese name of a person", and it is stored in the text file.&lt;/P&gt;&lt;P&gt;My problem is: I don't know how to&amp;nbsp;divide this seemingly random variable into the way I want?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As I said, I want to divide each row into&amp;nbsp;&lt;SPAN&gt;"ID1" + "Name1" + "ID2" + "Name2"&amp;nbsp;+&amp;nbsp;"ID3" + "Name3"&amp;nbsp;+&amp;nbsp;"ID4" + "Name4"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;As a whole, I wish the data looked like&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;ID1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Name1&amp;nbsp; &amp;nbsp;ID2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Name2&amp;nbsp; &amp;nbsp; &amp;nbsp; ID3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Name3&amp;nbsp; &amp;nbsp; ID4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Name4&lt;/SPAN&gt;&lt;/P&gt;&lt;DIV class="branch"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;30210027&amp;nbsp; &amp;nbsp; 甲三郎&amp;nbsp; &amp;nbsp; 30210397&amp;nbsp; &amp;nbsp; 乙先天冰&amp;nbsp; &amp;nbsp; 30210069&amp;nbsp; &amp;nbsp; 丙小姐&amp;nbsp; &amp;nbsp; 30210493&amp;nbsp; &amp;nbsp; 丁阿旺&lt;/P&gt;&lt;P&gt;30210044&amp;nbsp; &amp;nbsp; 物阿梅&amp;nbsp; &amp;nbsp; 30210021&amp;nbsp; &amp;nbsp; 己阿有&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 30240052&amp;nbsp; &amp;nbsp;更有言&amp;nbsp; &amp;nbsp; &amp;nbsp;30210346&amp;nbsp; &amp;nbsp; 心一?&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried three ways&lt;/P&gt;&lt;P&gt;#1 Import&lt;/P&gt;&lt;P&gt;It leads to a result that I wouldn't&amp;nbsp;conclude it as a failure, but it's definitely not a success:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="未命名.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/15417i507280C440ED570D/image-size/large?v=v2&amp;amp;px=999" role="button" title="未命名.png" alt="未命名.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;DIV class="branch"&gt;&lt;DIV align="center"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;#2 Infile&lt;/P&gt;&lt;P&gt;Some variables are wrong because of the random length of the Chinese name, plus there's no delimiter between each variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;#3 Adding delimiter, such as ",", by my hand.&lt;/P&gt;&lt;P&gt;For this method, I added a comma between each variable.&lt;/P&gt;&lt;P&gt;It works actually. However, considering the total of around 10000 variables awaits me to process, It may not work that well as I thought...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Therefore, I'm here to ask for your kindly help.&lt;/P&gt;&lt;P&gt;Any suggestions would be helpful.&lt;/P&gt;&lt;P&gt;Thanks in advance!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Sep 2017 06:54:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Divide-a-variable-into-several-variables/m-p/398736#M96483</guid>
      <dc:creator>Chung-Li</dc:creator>
      <dc:date>2017-09-26T06:54:54Z</dc:date>
    </item>
    <item>
      <title>Re: Divide a variable into several variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Divide-a-variable-into-several-variables/m-p/398744#M96486</link>
      <description>&lt;P&gt;I would go for the first one "import" which puts it all in one variable - var.&lt;/P&gt;
&lt;P&gt;Then i would walk through the dataset and for each line do a loop (do i = 1 to length(var)) and check each position in var, if its a numeric one then add it to a id variable and if it is a character put it as a name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Usually I always ask for delimiter in the files sent to system, to prevent this kind of logic &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;//fredrik&lt;/P&gt;</description>
      <pubDate>Tue, 26 Sep 2017 08:13:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Divide-a-variable-into-several-variables/m-p/398744#M96486</guid>
      <dc:creator>FredrikE</dc:creator>
      <dc:date>2017-09-26T08:13:03Z</dc:date>
    </item>
    <item>
      <title>Re: Divide a variable into several variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Divide-a-variable-into-several-variables/m-p/398745#M96487</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use a regex to add commas in your file each time a digit is followed by a non-digit&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Edit : and vice-versa&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example with sed :&lt;/P&gt;
&lt;P&gt;&amp;gt; cat &amp;gt; test &amp;lt;&amp;lt; EOF&lt;BR /&gt;&amp;gt; 012321akjghjkghj8576543jhgjhgjhg657654654kugjyfj&lt;BR /&gt;&amp;gt; 354354354jytjhfhgxchs9687657654jhgjhgjhgj65746576354yrzgredh&lt;BR /&gt;&amp;gt; EOF&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;gt; sed "s/\([0-9]\)\([^0-9]\)/$1,$2/g; s/\([^0-9]\)\([0-9]\)/$1,$2/g" test&lt;BR /&gt;01232,kjghjkgh,57654,hgjhgjh,5765465,ugjyfj&lt;BR /&gt;35435435,ytjhfhgxch,68765765,hgjhgjhg,574657635,rzgredh&lt;/P&gt;</description>
      <pubDate>Tue, 26 Sep 2017 08:28:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Divide-a-variable-into-several-variables/m-p/398745#M96487</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2017-09-26T08:28:45Z</dc:date>
    </item>
    <item>
      <title>Re: Divide a variable into several variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Divide-a-variable-into-several-variables/m-p/398778#M96494</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Take the LONG_TEXT variable and change all numbers to blanks, producing&amp;nbsp; a space-separated list of names in NAME_LIST&lt;/LI&gt;
&lt;LI&gt;As you iteratively extract each name, blank it out in LONG_TEXT, leaving a space-separated list of ID's:&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input long_text $60.;
datalines;
30210027aaaaaaaa30210397bbbb30210069cccc30210493dddd
run;

data want;
  set have;
  name_list=translate(long_text,' ','0123456789');
  array name {6} $10;
  do I=1 to countw(name_list);
    name{I}=scan(name_list,I,' ');
    long_text=transtrn(long_text,trim(name{I}),' ');
  end;
  array id{6};
  do I=1 to countw(long_text);
    id{I}=input(scan(long_text,I,' '),10.);
  end;&lt;BR /&gt;  drop name_text long_text;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Sep 2017 12:49:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Divide-a-variable-into-several-variables/m-p/398778#M96494</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-09-26T12:49:29Z</dc:date>
    </item>
    <item>
      <title>Re: Divide a variable into several variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Divide-a-variable-into-several-variables/m-p/398781#M96495</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards truncover;
input x $80.;
cards;
30210027甲三郎30210397乙先天冰30210069丙小姐30210493丁阿旺
30210044物阿梅30210021己阿有30240052更有言30210346心一?
;
run;
data want;
 set have;
 id1=scan(x,1,,'kd');
 name1=scan(x,1,,'d');
  id2=scan(x,2,,'kd');
 name2=scan(x,2,,'d');
  id3=scan(x,3,,'kd');
 name3=scan(x,3,,'d');
  id4=scan(x,4,,'kd');
 name4=scan(x,4,,'d');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Sep 2017 12:51:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Divide-a-variable-into-several-variables/m-p/398781#M96495</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-09-26T12:51:25Z</dc:date>
    </item>
    <item>
      <title>Re: Divide a variable into several variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Divide-a-variable-into-several-variables/m-p/399028#M96591</link>
      <description>Fredrik,&lt;BR /&gt;&lt;BR /&gt;Thank you for your help!&lt;BR /&gt;Sure, having delimiter can definitely make things easier, but sometimes things just don't go that well.</description>
      <pubDate>Wed, 27 Sep 2017 05:46:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Divide-a-variable-into-several-variables/m-p/399028#M96591</guid>
      <dc:creator>Chung-Li</dc:creator>
      <dc:date>2017-09-27T05:46:32Z</dc:date>
    </item>
    <item>
      <title>Re: Divide a variable into several variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Divide-a-variable-into-several-variables/m-p/399030#M96592</link>
      <description>Thanks Gamotte!&lt;BR /&gt;I'll check "regex".</description>
      <pubDate>Wed, 27 Sep 2017 05:53:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Divide-a-variable-into-several-variables/m-p/399030#M96592</guid>
      <dc:creator>Chung-Li</dc:creator>
      <dc:date>2017-09-27T05:53:15Z</dc:date>
    </item>
    <item>
      <title>Re: Divide a variable into several variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Divide-a-variable-into-several-variables/m-p/399035#M96594</link>
      <description>Mkeintz,&lt;BR /&gt;What a masterpiece this is!&lt;BR /&gt;Also, it works really well when I apply it to my data!&lt;BR /&gt;I guess now I need to spend some time to study this code.&lt;BR /&gt;&lt;BR /&gt;Thanks!</description>
      <pubDate>Wed, 27 Sep 2017 06:04:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Divide-a-variable-into-several-variables/m-p/399035#M96594</guid>
      <dc:creator>Chung-Li</dc:creator>
      <dc:date>2017-09-27T06:04:46Z</dc:date>
    </item>
    <item>
      <title>Re: Divide a variable into several variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Divide-a-variable-into-several-variables/m-p/399037#M96595</link>
      <description>Ksharp,&lt;BR /&gt;&lt;BR /&gt;This is so understandable for me, and it works!&lt;BR /&gt;Thank you so much!</description>
      <pubDate>Wed, 27 Sep 2017 06:06:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Divide-a-variable-into-several-variables/m-p/399037#M96595</guid>
      <dc:creator>Chung-Li</dc:creator>
      <dc:date>2017-09-27T06:06:45Z</dc:date>
    </item>
  </channel>
</rss>

