<?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: PROC dynamic format ..how if _n_ works? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-dynamic-format-how-if-n-works/m-p/242609#M45049</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/56119"&gt;@Rajeev8080&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;DIV class="yiv3829365231"&gt;The below is an example taken from google. how if _n_ works here. if i remind myself for PDV it should return only &amp;nbsp;1 entry as per condition _n_=1, how it returns all of below acctinfo values?&lt;/DIV&gt;
&lt;DIV class="yiv3829365231"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The data step have three main part&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if _n_=1 then do;
 put @1 "proc format;" ;
put @8 "value account";
 end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This part _surronded by DO and END_ will actually run one time, when reading the first observation from&amp;nbsp;&lt;SPAN&gt;acctinfo dataset. Where the condition IF (_n_= 1) is true.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The second part of the code is the one that&amp;nbsp;returns all of the acctinfo values, which run without any condition in each data step iteration.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; put @8 acctnum @20 "= '" name "'";
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Finally the part that run only when reading the last observation, where the condition IF (eof = true) is valid and that will happen only one time with you datset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; if eof then do;
 put @8 ";";
 put @1 "run;";
 end;&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;&amp;nbsp;So the code within the first condition if _n_=1 &amp;nbsp;is not the part that&amp;nbsp;&lt;SPAN&gt;returns all the&amp;nbsp;acctinfo values. But it will be valid for one time based on the condition and the two PUT statment will write to the file only one time.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 10 Jan 2016 18:04:14 GMT</pubDate>
    <dc:creator>mohamed_zaki</dc:creator>
    <dc:date>2016-01-10T18:04:14Z</dc:date>
    <item>
      <title>PROC dynamic format ..how if _n_ works?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-dynamic-format-how-if-n-works/m-p/242608#M45048</link>
      <description>&lt;DIV class="yiv3829365231"&gt;The below is an example taken from google. how if _n_ works here. if i remind myself for PDV it should return only &amp;nbsp;1 entry as per condition _n_=1, how it returns all of below acctinfo values?&lt;/DIV&gt;
&lt;DIV class="yiv3829365231"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="yiv3829365231"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="yiv3829365231"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;Approach 2 – Writing SAS code that is later used in the program&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;This approach employs PUT statement to generate SAS code in an external file and then uses %INCLUDE to bring&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;the generated code into the program. In the following example, an existing SAS data set (ACCTINFO) contains&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;account codes and corresponding customer names. There is one observation per account number. The observations&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;in the data set are as follows:&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;&amp;nbsp;ACCTNUM NAME&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;&amp;nbsp;5008074 John Smith&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;5008075 Bill Jones&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;&amp;nbsp;5009766 Benjamin Estes&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;.&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;.&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;.&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;Using the data in this data set, we want to create a format that converts an account code into a customer name. The&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;code can be set up as the following:&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;filename tmpfmt ‘external_file’;&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;data _null_;&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;&amp;nbsp;set acctinfo end=eof;&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;&amp;nbsp;file tmpfmt;&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;&amp;nbsp;if _n_=1 then do;&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;&amp;nbsp;put @1 "proc format;" ;&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;put @8 "value account";&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;&amp;nbsp;end;&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;&amp;nbsp;put @8 acctnum @20 "= '" name "'";&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;&amp;nbsp;if eof then do;&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;&amp;nbsp;put @8 ";";&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;&amp;nbsp;put @1 "run;";&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;&amp;nbsp;end;&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;run;&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;%include tmpfmt;&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;The dynamically created code produces:&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;proc format;&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;&amp;nbsp;value account&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;&amp;nbsp;5008074 = 'John Smith '&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;&amp;nbsp;5008075 = 'Bill Jones '&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;&amp;nbsp;5009766 = 'Benjamin Estes '&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;&amp;nbsp;.&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;&amp;nbsp;.&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;&amp;nbsp;.&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231"&gt;&amp;nbsp;;&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV id="yiv3829365231yui_3_16_0_1_1452444478715_3927" class="yiv3829365231" dir="ltr"&gt;&amp;nbsp;run;&lt;/DIV&gt;</description>
      <pubDate>Sun, 10 Jan 2016 17:16:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-dynamic-format-how-if-n-works/m-p/242608#M45048</guid>
      <dc:creator>Rajeev8080</dc:creator>
      <dc:date>2016-01-10T17:16:18Z</dc:date>
    </item>
    <item>
      <title>Re: PROC dynamic format ..how if _n_ works?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-dynamic-format-how-if-n-works/m-p/242609#M45049</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/56119"&gt;@Rajeev8080&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;DIV class="yiv3829365231"&gt;The below is an example taken from google. how if _n_ works here. if i remind myself for PDV it should return only &amp;nbsp;1 entry as per condition _n_=1, how it returns all of below acctinfo values?&lt;/DIV&gt;
&lt;DIV class="yiv3829365231"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The data step have three main part&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if _n_=1 then do;
 put @1 "proc format;" ;
put @8 "value account";
 end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This part _surronded by DO and END_ will actually run one time, when reading the first observation from&amp;nbsp;&lt;SPAN&gt;acctinfo dataset. Where the condition IF (_n_= 1) is true.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The second part of the code is the one that&amp;nbsp;returns all of the acctinfo values, which run without any condition in each data step iteration.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; put @8 acctnum @20 "= '" name "'";
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Finally the part that run only when reading the last observation, where the condition IF (eof = true) is valid and that will happen only one time with you datset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; if eof then do;
 put @8 ";";
 put @1 "run;";
 end;&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;&amp;nbsp;So the code within the first condition if _n_=1 &amp;nbsp;is not the part that&amp;nbsp;&lt;SPAN&gt;returns all the&amp;nbsp;acctinfo values. But it will be valid for one time based on the condition and the two PUT statment will write to the file only one time.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 10 Jan 2016 18:04:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-dynamic-format-how-if-n-works/m-p/242609#M45049</guid>
      <dc:creator>mohamed_zaki</dc:creator>
      <dc:date>2016-01-10T18:04:14Z</dc:date>
    </item>
    <item>
      <title>Re: PROC dynamic format ..how if _n_ works?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-dynamic-format-how-if-n-works/m-p/242613#M45050</link>
      <description>&lt;P&gt;It's great to see that you searched and found a solution that you're trying to understsand.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to mention that is not a good way to create a dynamic format.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use the CNTLIN to create a dataset without creating a new file and reading/executing it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example 8 in this paper demonstrates the method:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www2.sas.com/proceedings/sugi30/001-30.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi30/001-30.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a paper dedicated to that method alone:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www2.sas.com/proceedings/forum2007/068-2007.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/forum2007/068-2007.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 10 Jan 2016 18:47:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-dynamic-format-how-if-n-works/m-p/242613#M45050</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-01-10T18:47:48Z</dc:date>
    </item>
    <item>
      <title>Re: PROC dynamic format ..how if _n_ works?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-dynamic-format-how-if-n-works/m-p/243164#M45217</link>
      <description>&lt;P&gt;Ideally it should return 1 if we think of PDV . however, it returns many values with this condition while use in the PROC format that i like to use for further use.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2016 13:13:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-dynamic-format-how-if-n-works/m-p/243164#M45217</guid>
      <dc:creator>Rajeev8080</dc:creator>
      <dc:date>2016-01-13T13:13:12Z</dc:date>
    </item>
  </channel>
</rss>

