<?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: Data Manipulation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation/m-p/276347#M55352</link>
    <description>&lt;P&gt;Close but no cigar:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
set xyz;
by x;
retain z 0;
if first.x then z+1;
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 09 Jun 2016 18:38:09 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2016-06-09T18:38:09Z</dc:date>
    <item>
      <title>Data Manipulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation/m-p/276324#M55345</link>
      <description>&lt;P&gt;I want to add serial number to the unique values of each group. See the snapshot below-&lt;/P&gt;
&lt;P&gt;&lt;IMG src="https://4.bp.blogspot.com/-akOcFGkzVlI/V1cSOeNoKfI/AAAAAAAAEfU/wO1uRQBlgjgd-RiDx_m4ZD4Uh33POyK9wCLcB/s1600/datasets.png" border="0" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data xyz;
input x $;
cards;
AA
AA
AA
BB
BB
;
run;

proc sql;
select a.x, b.N from xyz a
inner join
(select x, monotonic() as N
from (
select distinct x
from xyz)) b
on a.x=b.x;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How we can&amp;nbsp;do it with data step?&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jun 2016 18:01:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation/m-p/276324#M55345</guid>
      <dc:creator>Ujjawal</dc:creator>
      <dc:date>2016-06-09T18:01:00Z</dc:date>
    </item>
    <item>
      <title>Re: Data Manipulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation/m-p/276333#M55347</link>
      <description>&lt;P&gt;&lt;A href="http://www.ats.ucla.edu/stat/sas/faq/enumerate.htm" target="_blank"&gt;http://www.ats.ucla.edu/stat/sas/faq/enumerate.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use BY group processing, see a detailed write up in the link above.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jun 2016 18:20:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation/m-p/276333#M55347</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-06-09T18:20:18Z</dc:date>
    </item>
    <item>
      <title>Re: Data Manipulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation/m-p/276337#M55350</link>
      <description>&lt;P&gt;I have tried but didn't get any success.&lt;BR /&gt;data temp;&lt;BR /&gt;set xyz;&lt;BR /&gt;if first.x then z=1;&lt;BR /&gt;else z+1;&lt;BR /&gt;by x;&lt;BR /&gt;proc print;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;It increments the value. It's not my desired output.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jun 2016 18:26:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation/m-p/276337#M55350</guid>
      <dc:creator>Ujjawal</dc:creator>
      <dc:date>2016-06-09T18:26:03Z</dc:date>
    </item>
    <item>
      <title>Re: Data Manipulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation/m-p/276347#M55352</link>
      <description>&lt;P&gt;Close but no cigar:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
set xyz;
by x;
retain z 0;
if first.x then z+1;
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 Jun 2016 18:38:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation/m-p/276347#M55352</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-06-09T18:38:09Z</dc:date>
    </item>
    <item>
      <title>Re: Data Manipulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation/m-p/276350#M55354</link>
      <description>&lt;P&gt;Thanks a ton. It works like a charm. How it works? I failed to understand how it increments with "if first.x then z+1;" and how it keeps the same value to values of a same group?&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jun 2016 19:01:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation/m-p/276350#M55354</guid>
      <dc:creator>Ujjawal</dc:creator>
      <dc:date>2016-06-09T19:01:28Z</dc:date>
    </item>
    <item>
      <title>Re: Data Manipulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation/m-p/276352#M55356</link>
      <description>&lt;P&gt;RETAIN is a datastep instruction that says to keep the value of a variable from record to another. This will not typically work if the variable(s) named in the Retain statement exist in a data set on the SET statement as they would be read from the set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using a statement like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Z+1; /* note there is no = in the assignment*/&lt;/P&gt;
&lt;P&gt;implies that Z should be retained.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jun 2016 19:06:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation/m-p/276352#M55356</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-06-09T19:06:50Z</dc:date>
    </item>
    <item>
      <title>Re: Data Manipulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation/m-p/276355#M55357</link>
      <description>&lt;P&gt;Thanks for your explanation.&amp;nbsp;I am confused why ELSE statement was not required. See my code below-&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data example22;&lt;BR /&gt;set xyz;&lt;BR /&gt;if first.x then N+1;&lt;BR /&gt;else N=N;&lt;BR /&gt;by x;&lt;BR /&gt;proc print;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jun 2016 19:14:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation/m-p/276355#M55357</guid>
      <dc:creator>Ujjawal</dc:creator>
      <dc:date>2016-06-09T19:14:06Z</dc:date>
    </item>
    <item>
      <title>Re: Data Manipulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation/m-p/276360#M55361</link>
      <description>&lt;P&gt;BY group processing:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a001283274.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a001283274.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;RETAIN&lt;/P&gt;
&lt;P&gt;keeps the value from row to row&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jun 2016 20:05:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation/m-p/276360#M55361</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-06-09T20:05:34Z</dc:date>
    </item>
    <item>
      <title>Re: Data Manipulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation/m-p/276363#M55362</link>
      <description>&lt;P&gt;Because you wanted the same value of&amp;nbsp;the counter&amp;nbsp;for all records with the same value of x you don't want an Else. The retain keeps the value once assigned.&lt;/P&gt;
&lt;P&gt;The ELSE clause would be for something you only want to do for records that are not the first.x.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See this thread for one example of Else with First.var processing to do something just a little more complicated that your problem:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Data-Management/Creating-a-counter-for-the-same-hospitalization/m-p/276010#M7767" target="_blank"&gt;https://communities.sas.com/t5/SAS-Data-Management/Creating-a-counter-for-the-same-hospitalization/m-p/276010#M7767&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jun 2016 20:45:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation/m-p/276363#M55362</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-06-09T20:45:03Z</dc:date>
    </item>
  </channel>
</rss>

