<?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: Conditional Nested Loops in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Nested-Loops/m-p/850639#M336168</link>
    <description>&lt;P&gt;Assuming you're new to SAS, one key point is that the SAS DATA step is a loop.&amp;nbsp; So you don't have to write a loop to loop through records in a dataset. A simple data step reads a one record from a dataset, processes the record, outputs it and then loops to read the next record.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sharing data as code makes it easier for people to help you.&amp;nbsp; You have data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input dates vins $5.;
  cards ;
 1880 4V6HJ
 1880 4V6HJ
 1880 6C8KI
 1881 4V6HJ
 1884 6C8KI
;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can create a counter with code like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have ;
  by dates vins ;
  if first.vins then counter++1 ;
  put (dates vins first.dates first.vins counter)(=) ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which will return:&lt;/P&gt;
&lt;PRE&gt;20   data want ;
21     set have ;
22     by dates vins ;
23     if first.vins then counter++1 ;
24     put (dates vins first.dates first.vins counter)(=) ;
25   run ;

dates=1880 vins=4V6HJ FIRST.dates=1 FIRST.vins=1 counter=1
dates=1880 vins=4V6HJ FIRST.dates=0 FIRST.vins=0 counter=1
dates=1880 vins=6C8KI FIRST.dates=0 FIRST.vins=1 counter=2
dates=1881 vins=4V6HJ FIRST.dates=1 FIRST.vins=1 counter=3
dates=1884 vins=6C8KI FIRST.dates=1 FIRST.vins=1 counter=4
NOTE: There were 5 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.WANT has 5 observations and 3 variables
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That simple data step makes use of a few novel SAS constructs, which are needed because the data step processes records one record at a time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first is BY-group processing.&amp;nbsp; The statement BY Dates Vins; tells SAS to create temporary boolean variables first.dates and first.vins which will be set to 1 (true) when the value&amp;nbsp; changes.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The statement counter++1; is an example of using the SUM statement to create an accumulator.&lt;/P&gt;</description>
    <pubDate>Wed, 21 Dec 2022 15:39:01 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2022-12-21T15:39:01Z</dc:date>
    <item>
      <title>Conditional Nested Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Nested-Loops/m-p/850633#M336166</link>
      <description>&lt;P&gt;Say I have a table of two columns of Dates and VINs, and I want to implement conditional nested loops to create a counter. The condition would essentially count the number of observations of unique VINs within a series of unique dates.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;DATES&amp;nbsp; &amp;nbsp; VINS&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;1880&amp;nbsp; &amp;nbsp; &amp;nbsp; 4V6HJ&lt;/P&gt;&lt;P&gt;&amp;nbsp;1880&amp;nbsp; &amp;nbsp; &amp;nbsp; 4V6HJ&lt;/P&gt;&lt;P&gt;&amp;nbsp;1880&amp;nbsp; &amp;nbsp; &amp;nbsp; 6C8KI&lt;/P&gt;&lt;P&gt;&amp;nbsp;1881&amp;nbsp; &amp;nbsp; &amp;nbsp; 4V6HJ&lt;/P&gt;&lt;P&gt;&amp;nbsp;1884&amp;nbsp; &amp;nbsp; &amp;nbsp; 6C8KI&lt;/P&gt;&lt;P&gt;For example, the above table would give a counter value of 4 because there are 2 distinct VINs within a distinct date and 2 distinct VINs in separate unique dates. I know how to generally implement this code in other languages, but I'm unaware of how to do this in SAS program within an EG project given varying indexing syntax and condition.&lt;/P&gt;&lt;P&gt;I suppose I would generally write it like this; it would be nice to see this generalized into SAS program.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i=1; n=0;&lt;/P&gt;&lt;P&gt;for 1:(length of column)-1&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; if date(i) = date(i+1)&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if vin(i) ~= vin(i+1)&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; n = n+1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; end&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else if date(i) ~= date(i+1)&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; n=n+1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; end&lt;/P&gt;&lt;P&gt;i = i+1;&lt;/P&gt;&lt;P&gt;end&lt;/P&gt;</description>
      <pubDate>Wed, 21 Dec 2022 15:13:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Nested-Loops/m-p/850633#M336166</guid>
      <dc:creator>EthanW1</dc:creator>
      <dc:date>2022-12-21T15:13:46Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Nested Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Nested-Loops/m-p/850639#M336168</link>
      <description>&lt;P&gt;Assuming you're new to SAS, one key point is that the SAS DATA step is a loop.&amp;nbsp; So you don't have to write a loop to loop through records in a dataset. A simple data step reads a one record from a dataset, processes the record, outputs it and then loops to read the next record.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sharing data as code makes it easier for people to help you.&amp;nbsp; You have data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input dates vins $5.;
  cards ;
 1880 4V6HJ
 1880 4V6HJ
 1880 6C8KI
 1881 4V6HJ
 1884 6C8KI
;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can create a counter with code like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have ;
  by dates vins ;
  if first.vins then counter++1 ;
  put (dates vins first.dates first.vins counter)(=) ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which will return:&lt;/P&gt;
&lt;PRE&gt;20   data want ;
21     set have ;
22     by dates vins ;
23     if first.vins then counter++1 ;
24     put (dates vins first.dates first.vins counter)(=) ;
25   run ;

dates=1880 vins=4V6HJ FIRST.dates=1 FIRST.vins=1 counter=1
dates=1880 vins=4V6HJ FIRST.dates=0 FIRST.vins=0 counter=1
dates=1880 vins=6C8KI FIRST.dates=0 FIRST.vins=1 counter=2
dates=1881 vins=4V6HJ FIRST.dates=1 FIRST.vins=1 counter=3
dates=1884 vins=6C8KI FIRST.dates=1 FIRST.vins=1 counter=4
NOTE: There were 5 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.WANT has 5 observations and 3 variables
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That simple data step makes use of a few novel SAS constructs, which are needed because the data step processes records one record at a time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first is BY-group processing.&amp;nbsp; The statement BY Dates Vins; tells SAS to create temporary boolean variables first.dates and first.vins which will be set to 1 (true) when the value&amp;nbsp; changes.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The statement counter++1; is an example of using the SUM statement to create an accumulator.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Dec 2022 15:39:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Nested-Loops/m-p/850639#M336168</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-12-21T15:39:01Z</dc:date>
    </item>
  </channel>
</rss>

