<?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: increment by subject using retain in a single datastep in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/increment-by-subject-using-retain-in-a-single-datastep/m-p/167289#M263906</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi all for providing helpful answers.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Jag&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 05 Jul 2014 13:39:00 GMT</pubDate>
    <dc:creator>Jagadishkatam</dc:creator>
    <dc:date>2014-07-05T13:39:00Z</dc:date>
    <item>
      <title>increment by subject using retain in a single datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/increment-by-subject-using-retain-in-a-single-datastep/m-p/167284#M263901</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Dear All,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Could you please help me to get the output as below in a single datastep. For the same i used two datasteps one to get the increment and other by merge statement. However i would like to know if there is another way to get the same result in a single datastep.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;input:&lt;/P&gt;&lt;P&gt;subj&amp;nbsp; month&lt;/P&gt;&lt;P&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; jan&lt;/P&gt;&lt;P&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; jan&lt;/P&gt;&lt;P&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; jan&lt;/P&gt;&lt;P&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; feb&lt;/P&gt;&lt;P&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; feb&lt;/P&gt;&lt;P&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; feb&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;output:&lt;/P&gt;&lt;P&gt;subj month seq&lt;/P&gt;&lt;P&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; jan&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; jan&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; jan&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; feb&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;/P&gt;&lt;P&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; feb&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;/P&gt;&lt;P&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; feb&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Jag&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 05 Jul 2014 03:27:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/increment-by-subject-using-retain-in-a-single-datastep/m-p/167284#M263901</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2014-07-05T03:27:53Z</dc:date>
    </item>
    <item>
      <title>Re: increment by subject using retain in a single datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/increment-by-subject-using-retain-in-a-single-datastep/m-p/167285#M263902</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;to get the above output, i tried something like below&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data have;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; input subj month$;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; order=_n_;&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; jan&lt;/P&gt;&lt;P&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; jan&lt;/P&gt;&lt;P&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; jan&lt;/P&gt;&lt;P&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; feb&lt;/P&gt;&lt;P&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; feb&lt;/P&gt;&lt;P&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; feb&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sort data = have out=have2 nodupkey;&lt;/P&gt;&lt;P&gt;by subj descending month;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have2 ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; by subj descending&amp;nbsp; month;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; retain seq;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if first.subj then seq=1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else seq+1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if last.month;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sort data = have;&lt;/P&gt;&lt;P&gt;by subj month;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sort data = want;&lt;/P&gt;&lt;P&gt;by subj month;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want2;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; merge have(in=a) want(in=b);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; by subj month;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Could you please make it simple.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Jag&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 05 Jul 2014 03:38:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/increment-by-subject-using-retain-in-a-single-datastep/m-p/167285#M263902</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2014-07-05T03:38:43Z</dc:date>
    </item>
    <item>
      <title>Re: increment by subject using retain in a single datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/increment-by-subject-using-retain-in-a-single-datastep/m-p/167286#M263903</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;data have;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; input subj month$;&lt;BR /&gt;cards;&lt;BR /&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; jan&lt;BR /&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; jan&lt;BR /&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; jan&lt;BR /&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; feb&lt;BR /&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; feb&lt;BR /&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; feb&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; by subj month notsorted;&lt;BR /&gt; if first.month then seq+1;&lt;BR /&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 05 Jul 2014 03:59:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/increment-by-subject-using-retain-in-a-single-datastep/m-p/167286#M263903</guid>
      <dc:creator>stat_sas</dc:creator>
      <dc:date>2014-07-05T03:59:29Z</dc:date>
    </item>
    <item>
      <title>Re: increment by subject using retain in a single datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/increment-by-subject-using-retain-in-a-single-datastep/m-p/167287#M263904</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Jag&lt;/P&gt;&lt;P&gt;Your main problem is you are entering month as a name rather than as a date, so the months will not sort properly.&amp;nbsp; The solution above works for a single subj provided all the months are in the same year and the data is in intuitive order. To generalise to more than one subject you need to reset the counter for each subj.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 12.800000190734863px; background-color: #ffffff;"&gt;data want;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 12.800000190734863px; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 12.800000190734863px; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; by subj month notsorted;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 12.800000190734863px; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; retain seq ;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 12.800000190734863px; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if first.subj then seq=0 ;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 12.800000190734863px; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if first.month then seq+1;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 12.800000190734863px; background-color: #ffffff;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 12.800000190734863px; background-color: #ffffff;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 12.800000190734863px; background-color: #ffffff;"&gt;Richard&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 05 Jul 2014 04:13:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/increment-by-subject-using-retain-in-a-single-datastep/m-p/167287#M263904</guid>
      <dc:creator>RichardinOz</dc:creator>
      <dc:date>2014-07-05T04:13:58Z</dc:date>
    </item>
    <item>
      <title>Re: increment by subject using retain in a single datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/increment-by-subject-using-retain-in-a-single-datastep/m-p/167288#M263905</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I assume you want such a sequence number to be able to order your data per ID and Month. Ideally your "month" variable would be numeric containing a SAS date value.&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;Below an approach which allows you to create a month number based on the month name. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data work.fmt_source (drop= _:);&lt;/P&gt;&lt;P&gt;&amp;nbsp; fmtname='Mon_Seq';&lt;/P&gt;&lt;P&gt;&amp;nbsp; type='I';&lt;/P&gt;&lt;P&gt;&amp;nbsp; _start_dt='01jan1960'd;&lt;/P&gt;&lt;P&gt;&amp;nbsp; do label=1 to 12;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; start=put(intnx('month',_start_dt,label-1,'s'),monname3.);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; output;&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc format cntlin=work.fmt_source;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data sample;&lt;/P&gt;&lt;P&gt;&amp;nbsp; input subj month $;&lt;/P&gt;&lt;P&gt;&amp;nbsp; seq=input(propcase(month),Mon_Seq.);&lt;/P&gt;&lt;P&gt;&amp;nbsp; cards;&lt;/P&gt;&lt;P&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; jan&lt;/P&gt;&lt;P&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; jan&lt;/P&gt;&lt;P&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; jan&lt;/P&gt;&lt;P&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; feb&lt;/P&gt;&lt;P&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; feb&lt;/P&gt;&lt;P&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; feb&lt;/P&gt;&lt;P&gt;101&amp;nbsp;&amp;nbsp;&amp;nbsp; jan&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 05 Jul 2014 04:51:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/increment-by-subject-using-retain-in-a-single-datastep/m-p/167288#M263905</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2014-07-05T04:51:52Z</dc:date>
    </item>
    <item>
      <title>Re: increment by subject using retain in a single datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/increment-by-subject-using-retain-in-a-single-datastep/m-p/167289#M263906</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi all for providing helpful answers.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Jag&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 05 Jul 2014 13:39:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/increment-by-subject-using-retain-in-a-single-datastep/m-p/167289#M263906</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2014-07-05T13:39:00Z</dc:date>
    </item>
    <item>
      <title>Re: increment by subject using retain in a single datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/increment-by-subject-using-retain-in-a-single-datastep/m-p/167290#M263907</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Im having a query &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;dataset test consists of a numeric variable X&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;X&lt;/P&gt;&lt;P&gt;----------&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;2&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;2&lt;/P&gt;&lt;P&gt;2&lt;/P&gt;&lt;P&gt;3&lt;/P&gt;&lt;P&gt;3&lt;/P&gt;&lt;P&gt;4&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;when ever sas reads the first value of X a new variable &lt;/P&gt;&lt;P&gt;Y should contain the same value.&lt;/P&gt;&lt;P&gt;Then again if the 2nd observation is same as first observation value of Y should increment accordingly&lt;/P&gt;&lt;P&gt;If a value is not as not the previous one &lt;/P&gt;&lt;P&gt;eg if X=2 is encountered then Y value should again start from 1 and increment by 1 accordingly.&lt;/P&gt;&lt;P&gt;the process should go on till end of file.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Output table should look like&lt;/P&gt;&lt;P&gt;-----------------------------------&lt;/P&gt;&lt;P&gt;X&amp;nbsp;&amp;nbsp; Y&lt;/P&gt;&lt;P&gt;-----------------------------------&lt;/P&gt;&lt;P&gt;1 1&lt;/P&gt;&lt;P&gt;1 2&lt;/P&gt;&lt;P&gt;1 3&lt;/P&gt;&lt;P&gt;2 1&lt;/P&gt;&lt;P&gt;1 4&lt;/P&gt;&lt;P&gt;1 5&lt;/P&gt;&lt;P&gt;2 2&lt;/P&gt;&lt;P&gt;2 3&lt;/P&gt;&lt;P&gt;3 1&lt;/P&gt;&lt;P&gt;3 2&lt;/P&gt;&lt;P&gt;4 1&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;kindly help me to write a sas code in this&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 01 Dec 2014 11:00:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/increment-by-subject-using-retain-in-a-single-datastep/m-p/167290#M263907</guid>
      <dc:creator>sne7189</dc:creator>
      <dc:date>2014-12-01T11:00:13Z</dc:date>
    </item>
    <item>
      <title>Re: increment by subject using retain in a single datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/increment-by-subject-using-retain-in-a-single-datastep/m-p/167291#M263908</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Im having a query &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;dataset test consists of a numeric variable X&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;X&lt;/P&gt;&lt;P&gt;----------&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;2&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;2&lt;/P&gt;&lt;P&gt;2&lt;/P&gt;&lt;P&gt;3&lt;/P&gt;&lt;P&gt;3&lt;/P&gt;&lt;P&gt;4&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;when ever sas reads the first value of X a new variable &lt;/P&gt;&lt;P&gt;Y should contain the same value.&lt;/P&gt;&lt;P&gt;Then again if the 2nd observation is same as first observation value of Y should increment accordingly&lt;/P&gt;&lt;P&gt;If a value is not as not the previous one &lt;/P&gt;&lt;P&gt;eg if X=2 is encountered then Y value should again start from 1 and increment by 1 accordingly.&lt;/P&gt;&lt;P&gt;the process should go on till end of file.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Output table should look like&lt;/P&gt;&lt;P&gt;-----------------------------------&lt;/P&gt;&lt;P&gt;X&amp;nbsp;&amp;nbsp; Y&lt;/P&gt;&lt;P&gt;-----------------------------------&lt;/P&gt;&lt;P&gt;1 1&lt;/P&gt;&lt;P&gt;1 2&lt;/P&gt;&lt;P&gt;1 3&lt;/P&gt;&lt;P&gt;2 1&lt;/P&gt;&lt;P&gt;1 4&lt;/P&gt;&lt;P&gt;1 5&lt;/P&gt;&lt;P&gt;2 2&lt;/P&gt;&lt;P&gt;2 3&lt;/P&gt;&lt;P&gt;3 1&lt;/P&gt;&lt;P&gt;3 2&lt;/P&gt;&lt;P&gt;4 1&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;kindly help me to write a sas code in this&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 01 Dec 2014 11:00:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/increment-by-subject-using-retain-in-a-single-datastep/m-p/167291#M263908</guid>
      <dc:creator>sne7189</dc:creator>
      <dc:date>2014-12-01T11:00:48Z</dc:date>
    </item>
    <item>
      <title>Re: increment by subject using retain in a single datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/increment-by-subject-using-retain-in-a-single-datastep/m-p/167292#M263909</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Please try&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data have;&lt;/P&gt;&lt;P&gt;input x;&lt;/P&gt;&lt;P&gt;ord=_n_;&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;2&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;2&lt;/P&gt;&lt;P&gt;2&lt;/P&gt;&lt;P&gt;3&lt;/P&gt;&lt;P&gt;3&lt;/P&gt;&lt;P&gt;4&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sort data=have;&lt;/P&gt;&lt;P&gt;by x;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;by x;&lt;/P&gt;&lt;P&gt;if first.x then y=1;&lt;/P&gt;&lt;P&gt;else y+1;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sort data=want;&lt;/P&gt;&lt;P&gt;by ord;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Jag&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 01 Dec 2014 13:32:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/increment-by-subject-using-retain-in-a-single-datastep/m-p/167292#M263909</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2014-12-01T13:32:03Z</dc:date>
    </item>
  </channel>
</rss>

