<?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: What does these codes do? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/What-does-these-codes-do/m-p/262158#M269126</link>
    <description>&lt;P&gt;No, this piece of code is written by my professor for demonstration purposes. It has no practical use. It is there to show us how to use a double trailing&amp;nbsp;@@ to input the datalines as shown.&lt;/P&gt;</description>
    <pubDate>Thu, 07 Apr 2016 18:11:39 GMT</pubDate>
    <dc:creator>junlue</dc:creator>
    <dc:date>2016-04-07T18:11:39Z</dc:date>
    <item>
      <title>What does these codes do?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-does-these-codes-do/m-p/261842#M269120</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data _NULL_ (drop=TeamName);&lt;BR /&gt;length Event3 Event2 Event1 8. TeamName ParticipantName $8. ;&lt;BR /&gt;format Event2 z8. ;&lt;BR /&gt;&lt;STRONG&gt;retain&lt;/STRONG&gt; TeamName Count ;&lt;/P&gt;&lt;P&gt;input TeamName $ Count &lt;STRONG&gt;@&lt;/STRONG&gt; ;&lt;/P&gt;&lt;P&gt;do i = 1 to Count ;&lt;BR /&gt;input ParticipantName $ Event1 Event2 Event3 @@ ;&lt;BR /&gt;TeamTotal + (Event1 + Event2 + Event3);&lt;BR /&gt;output ;&lt;/P&gt;&lt;P&gt;FILE 'C:\Users\user\Desktop\IDS SAS 594\samplefile.txt' ;&lt;BR /&gt;if &lt;STRONG&gt;_N_&lt;/STRONG&gt;=3 then do ;&lt;BR /&gt;if i = 1 then do ;&lt;BR /&gt;put TeamName Count @@ ;&lt;BR /&gt;end ;&lt;BR /&gt;&lt;STRONG&gt;if i &amp;lt; 2 then do&lt;/STRONG&gt; ;&lt;BR /&gt;put ParticipantName Event1 Event2 Event3 @@ ;&lt;BR /&gt;end ;&lt;BR /&gt;else do ;&lt;BR /&gt;put ParticipantName Event1 Event2 Event3 ;&lt;BR /&gt;end ;&lt;BR /&gt;end ;&lt;BR /&gt;else do ;&lt;BR /&gt;put TeamName Count ;&lt;BR /&gt;put ParticipantName Event1 Event2 Event3 ;&lt;BR /&gt;end ;&lt;BR /&gt;end ;&lt;/P&gt;&lt;P&gt;datalines;&lt;BR /&gt;Knights 1&lt;BR /&gt;Sue 6 8 8&lt;BR /&gt;Kings 1&lt;BR /&gt;Jane 9 7 8&lt;BR /&gt;Knights 4 John 7 7 7 Lisa 8 9 9&lt;BR /&gt;Fran 7 6 6&lt;BR /&gt;Walter 9 8 10&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do not understand the bold parts.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Help&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Apr 2016 16:56:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-does-these-codes-do/m-p/261842#M269120</guid>
      <dc:creator>junlue</dc:creator>
      <dc:date>2016-04-06T16:56:06Z</dc:date>
    </item>
    <item>
      <title>Re: What does these codes do?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-does-these-codes-do/m-p/261857#M269121</link>
      <description>&lt;P&gt;Retain creates and identifies a variable whose values are kept from one observation (record) to the next.&lt;/P&gt;
&lt;P&gt;_N_ is an automatic variable that SAS supplies to identify the record .&lt;/P&gt;
&lt;P&gt;So if &lt;STRONG&gt;_N_&lt;/STRONG&gt;=3 then do&amp;nbsp; is "When this is the third record do something"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You have a group of statements done for values of 1 to the value of count on the current record that starts with : do i = 1 to Count ; to the END; just before datalines.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;if i &amp;lt; 2 then do&lt;/STRONG&gt; ; If Count is 2 or larger then the loop will increment and the counter variable i, when the counter is less than 2 point it prints&amp;nbsp; the values of some variables to the log.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since i will have values like 1 , 2 ,3 the same effect could be done with i=1 to print on the first pass through the loop.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Apr 2016 17:25:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-does-these-codes-do/m-p/261857#M269121</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-04-06T17:25:20Z</dc:date>
    </item>
    <item>
      <title>Re: What does these codes do?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-does-these-codes-do/m-p/261921#M269122</link>
      <description>&lt;P&gt;Concerning "if _N_ = 3 do"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But I thought that SAS Data step updates _N_ after the iteration.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Meaning when the data step iterates to the 3rd recound, _N_ is still equal to 2. Only after finishing the 3rd record do _N_ gets updated to 3?&lt;/P&gt;</description>
      <pubDate>Wed, 06 Apr 2016 19:53:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-does-these-codes-do/m-p/261921#M269122</guid>
      <dc:creator>junlue</dc:creator>
      <dc:date>2016-04-06T19:53:24Z</dc:date>
    </item>
    <item>
      <title>Re: What does these codes do?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-does-these-codes-do/m-p/261925#M269123</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Actually, the interpretation of _N_ is a bit tricky here: It counts the &lt;EM&gt;iterations of the data step&lt;/EM&gt;. In&amp;nbsp;many cases this coincides with the "record number," as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw﻿&lt;/a&gt;&amp;nbsp;pointed out. In the present data step, however,&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;_N_=1 for the first two input records, which in turn go into the first output record (replace _NULL_ by a dataset name to check), but correspond to the first two records in samplefile.txt.&lt;/LI&gt;
&lt;LI&gt;_N_=2&amp;nbsp;&lt;SPAN&gt;for the 3rd and 4th input record, which in turn go into the second output record and which correspond to the 3rd and 4th line &lt;SPAN&gt;in samplefile.txt.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;_N_=3 for the 5th, 6th and 7th input record, which in turn populate output records no. 3, 4, 5 and 6 and are written to lines 5, 6 and 7 of&amp;nbsp;samplefile.txt.&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;Edit: The very last value of _N_ is even 4: immediately before the first INPUT statement of the data step hits the semicolon after the last data line, which terminates the data step.&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&lt;SPAN&gt;(See documentation on "&lt;A href="http://support.sas.com/documentation/cdl/en/lrcon/68089/HTML/default/viewer.htm#p0e0mk25gs9binn1s9jiu4otau29.htm" target="_blank"&gt;Automatic Variables&lt;/A&gt;.")&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just for completeness: The single trailing @&amp;nbsp;at the end of the first INPUT statament "holds an input record for the execution of the next INPUT statement within the same iteration of the DATA step" (&lt;A href="http://support.sas.com/documentation/cdl/en/syntaxidx/68719/HTML/default/index.htm#/documentation/cdl//en/lestmtsref/68024/HTML/default/n0oaql83drile0n141pdacojq97s.htm" target="_blank"&gt;INPUT statement documentation&lt;/A&gt;). This is relevant here when the fifth data line is being read. After reading "Knights 4" the pointer must stay on the same input record to&amp;nbsp;let the next INPUT statement read "John" etc. and not "Fran" (ignoring the rest of the previous line).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Apr 2016 20:19:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-does-these-codes-do/m-p/261925#M269123</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-04-06T20:19:29Z</dc:date>
    </item>
    <item>
      <title>Re: What does these codes do?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-does-these-codes-do/m-p/261996#M269124</link>
      <description>&lt;P&gt;All posters:&lt;/P&gt;
&lt;P&gt;Look thoroughly at that data step, it reads from DATALINES and writes to the file in the FILE statement. What is especially puzzling is that a keep/drop option is used on _NULL_ and a completely useless OUTPUT statement is present.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2016 05:46:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-does-these-codes-do/m-p/261996#M269124</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-04-07T05:46:39Z</dc:date>
    </item>
    <item>
      <title>Re: What does these codes do?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-does-these-codes-do/m-p/261999#M269125</link>
      <description>&lt;P&gt;First, put some proper formatting into this horrible piece of spaghetti code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test /*(drop=TeamName)*/;
file '$HOME/sascommunity/samplefile.txt' ;
length Event3 Event2 Event1 8. TeamName ParticipantName $8.;
format Event2 z8.;
retain TeamName Count;

input TeamName $ Count @;

do i = 1 to Count;
  input ParticipantName $ Event1 Event2 Event3 @@;
  TeamTotal + (Event1 + Event2 + Event3);
  output;

  if _N_ = 3 then do;
    if i = 1 then do;
      put TeamName Count @@;
    end;
    if i &amp;lt; 2 then do;
      put ParticipantName Event1 Event2 Event3 @@;
    end ;
    else do ;
      put ParticipantName Event1 Event2 Event3;
    end;
  end;
  else do;
    put TeamName Count;
    put ParticipantName Event1 Event2 Event3;
  end;
end;

datalines;
Knights 1
Sue 6 8 8
Kings 1
Jane 9 7 8
Knights 4 John 7 7 7 Lisa 8 9 9
Fran 7 6 6
Walter 9 8 10
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now it becomes easier to see that the if _N_ = 3 block is only there to recreate the layout of the datalines in the output file (more or less, as event2 now has a z8. format).&lt;/P&gt;
&lt;P&gt;Does this have ANY practical use?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2016 06:40:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-does-these-codes-do/m-p/261999#M269125</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-04-07T06:40:21Z</dc:date>
    </item>
    <item>
      <title>Re: What does these codes do?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-does-these-codes-do/m-p/262158#M269126</link>
      <description>&lt;P&gt;No, this piece of code is written by my professor for demonstration purposes. It has no practical use. It is there to show us how to use a double trailing&amp;nbsp;@@ to input the datalines as shown.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2016 18:11:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-does-these-codes-do/m-p/262158#M269126</guid>
      <dc:creator>junlue</dc:creator>
      <dc:date>2016-04-07T18:11:39Z</dc:date>
    </item>
    <item>
      <title>Re: What does these codes do?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-does-these-codes-do/m-p/262294#M269127</link>
      <description>&lt;P&gt;Next, look at these pieces of code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i = 1 to Count;
  .......

  if _N_ = 3 then do;
    if i = 1 then do;
      put TeamName Count @@;
    end;
    if i &amp;lt; 2 then do;
 &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;One can easily see that the variable i will only have ordinal values of 1,2,3,.....&lt;/P&gt;
&lt;P&gt;Therefore the condition i &amp;lt; 2 is equivalent to i = 1, and the two blocks can be merged into one with the same functionality, which would also improve readability.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Apr 2016 05:29:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-does-these-codes-do/m-p/262294#M269127</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-04-08T05:29:57Z</dc:date>
    </item>
  </channel>
</rss>

