<?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: Cleaning data by getting the value into the correct variable and add a row of data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Cleaning-data-by-getting-the-value-into-the-correct-variable-and/m-p/753791#M237638</link>
    <description>&lt;P&gt;Try this: (it should work even if there are more than 2 commas... even though you say that won't happen, you get the functionality at no extra charge!)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
Input Name $ Sex $ units &amp;amp; $100.;
cards;
Amy F 103 ,104
Bob M 203 A
Carolin F 204 B,205B
David M 208 ,210
Emily F 513 C,515
Run;
data want;
    set have;
    length thisstr $ 100;
    units=compress(units);
    thisstr='ZZZZ';
    count=0;
    do while(not missing(thisstr));
        count=count+1;
        thisstr=scan(units,count,',');
        call missing(unit_number);
        unit_alpha=' ';
        where = anyalpha(thisstr);
        if where=0 then unit_number=thisstr;
        else do;
            unit_number=substr(thisstr,1,where-1);
            unit_alpha=substr(thisstr,where);
        end;
        if not missing(thisstr) then output;
    end;
    keep name sex unit_number unit_alpha;
run;

        &lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 13 Jul 2021 15:00:22 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2021-07-13T15:00:22Z</dc:date>
    <item>
      <title>Cleaning data by getting the value into the correct variable and add a row of data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cleaning-data-by-getting-the-value-into-the-correct-variable-and/m-p/753780#M237629</link>
      <description>I have a set of data, the variable unit_number is supposed to contain only the numeric data, and unit_alphabet is supposed to contain only the one character data. However the original data may contain unit_number information in the unit_alpha variable to indicate that there are two unit_number information. If there is more than one unit_number information, there will be a comma followed by number in the unit_alphabet variable to separate the unit_number information. And I want each row of data only contain one unit number information, if the unit_number information is more than one (so there is a comma in the unit_alphabet variable followed by number), I want to add another row of data to indicate this. And it is possible there there are two character in the unit_alphabet variable, I want the first character to be assigned to the first unit_number, and the second character to be assigned to the second unit_number.&lt;BR /&gt;&lt;BR /&gt;The data set I have:&lt;BR /&gt;Data have;&lt;BR /&gt;Input Name $ Sex $ Unit_number $ unit_alphabet$: $20.;&lt;BR /&gt;cards;&lt;BR /&gt;Amy F 103 ,104&lt;BR /&gt;Bob M 203 A&lt;BR /&gt;Carolin F 204 B,205B&lt;BR /&gt;David M 208 ,210&lt;BR /&gt;Emily F 513 C,515&lt;BR /&gt;Run;&lt;BR /&gt;&lt;BR /&gt;I want the data set looks like:&lt;BR /&gt;Name Sex Unit_Number Unit_Alphabet&lt;BR /&gt;Amy F 103&lt;BR /&gt;Amy F 104&lt;BR /&gt;Bob M 203 A&lt;BR /&gt;Carolin F 204 B&lt;BR /&gt;Carolin F 205 B&lt;BR /&gt;David M 208&lt;BR /&gt;David M 210&lt;BR /&gt;Emily F 513 C&lt;BR /&gt;Emily F 515&lt;BR /&gt;&lt;BR /&gt;Thank you!!</description>
      <pubDate>Tue, 13 Jul 2021 14:21:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cleaning-data-by-getting-the-value-into-the-correct-variable-and/m-p/753780#M237629</guid>
      <dc:creator>apeeape</dc:creator>
      <dc:date>2021-07-13T14:21:08Z</dc:date>
    </item>
    <item>
      <title>Re: Cleaning data by getting the value into the correct variable and add a row of data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cleaning-data-by-getting-the-value-into-the-correct-variable-and/m-p/753783#M237630</link>
      <description>&lt;P&gt;So, is this really the way the actual data appears, with inconsistent spacing before the comma (sometimes there is a space and sometimes not) and no indication of a missing value for some records; or is it just poor typing?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Would simply removing the blanks after the person's sex (in the unit number or unit alphabet variable) cause problems?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can there ever be more than one comma, indicating the result would have to wind up on 3 or more lines?&lt;/P&gt;</description>
      <pubDate>Tue, 13 Jul 2021 14:41:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cleaning-data-by-getting-the-value-into-the-correct-variable-and/m-p/753783#M237630</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-07-13T14:41:26Z</dc:date>
    </item>
    <item>
      <title>Re: Cleaning data by getting the value into the correct variable and add a row of data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cleaning-data-by-getting-the-value-into-the-correct-variable-and/m-p/753785#M237632</link>
      <description>Yes this is the way the actual data appears, all the data after the space is under the unit_alphabet variable. Eg(“,104”,”B,205B”,”,210”). Some data may not have unit_alphabet records. The maximum number of comma is one, so the result will not be more than 2 lines for each data. Thank you!</description>
      <pubDate>Tue, 13 Jul 2021 14:45:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cleaning-data-by-getting-the-value-into-the-correct-variable-and/m-p/753785#M237632</guid>
      <dc:creator>apeeape</dc:creator>
      <dc:date>2021-07-13T14:45:57Z</dc:date>
    </item>
    <item>
      <title>Re: Cleaning data by getting the value into the correct variable and add a row of data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cleaning-data-by-getting-the-value-into-the-correct-variable-and/m-p/753791#M237638</link>
      <description>&lt;P&gt;Try this: (it should work even if there are more than 2 commas... even though you say that won't happen, you get the functionality at no extra charge!)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
Input Name $ Sex $ units &amp;amp; $100.;
cards;
Amy F 103 ,104
Bob M 203 A
Carolin F 204 B,205B
David M 208 ,210
Emily F 513 C,515
Run;
data want;
    set have;
    length thisstr $ 100;
    units=compress(units);
    thisstr='ZZZZ';
    count=0;
    do while(not missing(thisstr));
        count=count+1;
        thisstr=scan(units,count,',');
        call missing(unit_number);
        unit_alpha=' ';
        where = anyalpha(thisstr);
        if where=0 then unit_number=thisstr;
        else do;
            unit_number=substr(thisstr,1,where-1);
            unit_alpha=substr(thisstr,where);
        end;
        if not missing(thisstr) then output;
    end;
    keep name sex unit_number unit_alpha;
run;

        &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 13 Jul 2021 15:00:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cleaning-data-by-getting-the-value-into-the-correct-variable-and/m-p/753791#M237638</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-07-13T15:00:22Z</dc:date>
    </item>
    <item>
      <title>Re: Cleaning data by getting the value into the correct variable and add a row of data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cleaning-data-by-getting-the-value-into-the-correct-variable-and/m-p/753793#M237640</link>
      <description>This works out perfectly!! Thank you very much for your help!</description>
      <pubDate>Tue, 13 Jul 2021 15:07:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cleaning-data-by-getting-the-value-into-the-correct-variable-and/m-p/753793#M237640</guid>
      <dc:creator>apeeape</dc:creator>
      <dc:date>2021-07-13T15:07:58Z</dc:date>
    </item>
  </channel>
</rss>

