<?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: Number of observation in dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-in-dataset/m-p/839336#M331880</link>
    <description>&lt;P&gt;From your code, it looks like you might be running this DATA step in CAS.&amp;nbsp; In CAS tables, there are no "observation numbers" because there is not an intrinsic order to the rows. They can be processed in any order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In Viya 4, you can use the ADDROWID=YES option to add a special variable called _ROWID_&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/v_026/ledsoptsref/n0lfk3elmwkn7kn1g11nxyjnqam7.htm" target="_blank"&gt;SAS Help Center: ADDROWID= Data Set Option&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In earlier versions of CAS, you can combine the _N_ and the _THREADID_ variables to get a unique ID. See&lt;/P&gt;
&lt;P&gt;&lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/casdspgm/p1myr5r0z9n55vn1c0ijy5wt6f8g.htm#n1fgpzk1m0yisyn1i1cnsu1143rg" target="_blank" rel="noopener"&gt;https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/casdspgm/p1myr5r0z9n55vn1c0ijy5wt6f8g.htm#n1fgpzk1m0yisyn1i1cnsu1143rg&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;and&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/casdspgm/p10ux48pz7qknzn1ux1bs48vplnx.htm#p1o13jlo1hed7kn1qhb24c3pvd7i" target="_blank" rel="noopener"&gt;https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/casdspgm/p10ux48pz7qknzn1ux1bs48vplnx.htm#p1o13jlo1hed7kn1qhb24c3pvd7i&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 19 Oct 2022 12:35:48 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2022-10-19T12:35:48Z</dc:date>
    <item>
      <title>Number of observation in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-in-dataset/m-p/839316#M331868</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a table that may contain 1 to 5 observations, i need to add a column that contains the number of observations.&lt;/P&gt;
&lt;P&gt;i tried row_num= _n_ but it is not what i want.&lt;/P&gt;
&lt;P&gt;i will be thankful for your help.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Oct 2022 09:49:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-in-dataset/m-p/839316#M331868</guid>
      <dc:creator>aloou</dc:creator>
      <dc:date>2022-10-19T09:49:49Z</dc:date>
    </item>
    <item>
      <title>Re: Number of observation in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-in-dataset/m-p/839318#M331869</link>
      <description>&lt;P&gt;You want the number of observations in every row, correct?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set sashelp.class nobs = nobs;
   n = nobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Oct 2022 09:55:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-in-dataset/m-p/839318#M331869</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-10-19T09:55:52Z</dc:date>
    </item>
    <item>
      <title>Re: Number of observation in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-in-dataset/m-p/839322#M331872</link>
      <description>No not the number of observations in every row.&lt;BR /&gt;like for example the table contains 5 observations , i want the new column to be like :&lt;BR /&gt;1 &lt;BR /&gt;2 &lt;BR /&gt;3&lt;BR /&gt;4&lt;BR /&gt;5</description>
      <pubDate>Wed, 19 Oct 2022 10:02:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-in-dataset/m-p/839322#M331872</guid>
      <dc:creator>aloou</dc:creator>
      <dc:date>2022-10-19T10:02:18Z</dc:date>
    </item>
    <item>
      <title>Re: Number of observation in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-in-dataset/m-p/839323#M331873</link>
      <description>&lt;P&gt;Then your initial attempt is good&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set sashelp.class;
   n = _N_;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Oct 2022 10:03:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-in-dataset/m-p/839323#M331873</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-10-19T10:03:36Z</dc:date>
    </item>
    <item>
      <title>Re: Number of observation in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-in-dataset/m-p/839324#M331874</link>
      <description>i tried it again in a table with two observations, it writes &lt;BR /&gt;1&lt;BR /&gt;1</description>
      <pubDate>Wed, 19 Oct 2022 10:07:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-in-dataset/m-p/839324#M331874</guid>
      <dc:creator>aloou</dc:creator>
      <dc:date>2022-10-19T10:07:18Z</dc:date>
    </item>
    <item>
      <title>Re: Number of observation in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-in-dataset/m-p/839326#M331876</link>
      <description>&lt;P&gt;Show me your code?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The _N_ variable gives you the number of times the data step has iterated. Not the input observation number.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Oct 2022 10:17:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-in-dataset/m-p/839326#M331876</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-10-19T10:17:18Z</dc:date>
    </item>
    <item>
      <title>Re: Number of observation in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-in-dataset/m-p/839328#M331877</link>
      <description>data casuser.test;&lt;BR /&gt;   set CASUSER.CHOIXC;&lt;BR /&gt;   n = _N_;&lt;BR /&gt;run;&lt;BR /&gt;i need to put the observation number and not how many times it iterated.&lt;BR /&gt;</description>
      <pubDate>Wed, 19 Oct 2022 10:22:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-in-dataset/m-p/839328#M331877</guid>
      <dc:creator>aloou</dc:creator>
      <dc:date>2022-10-19T10:22:19Z</dc:date>
    </item>
    <item>
      <title>Re: Number of observation in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-in-dataset/m-p/839336#M331880</link>
      <description>&lt;P&gt;From your code, it looks like you might be running this DATA step in CAS.&amp;nbsp; In CAS tables, there are no "observation numbers" because there is not an intrinsic order to the rows. They can be processed in any order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In Viya 4, you can use the ADDROWID=YES option to add a special variable called _ROWID_&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/v_026/ledsoptsref/n0lfk3elmwkn7kn1g11nxyjnqam7.htm" target="_blank"&gt;SAS Help Center: ADDROWID= Data Set Option&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In earlier versions of CAS, you can combine the _N_ and the _THREADID_ variables to get a unique ID. See&lt;/P&gt;
&lt;P&gt;&lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/casdspgm/p1myr5r0z9n55vn1c0ijy5wt6f8g.htm#n1fgpzk1m0yisyn1i1cnsu1143rg" target="_blank" rel="noopener"&gt;https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/casdspgm/p1myr5r0z9n55vn1c0ijy5wt6f8g.htm#n1fgpzk1m0yisyn1i1cnsu1143rg&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;and&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/casdspgm/p10ux48pz7qknzn1ux1bs48vplnx.htm#p1o13jlo1hed7kn1qhb24c3pvd7i" target="_blank" rel="noopener"&gt;https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/casdspgm/p10ux48pz7qknzn1ux1bs48vplnx.htm#p1o13jlo1hed7kn1qhb24c3pvd7i&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Oct 2022 12:35:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-in-dataset/m-p/839336#M331880</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2022-10-19T12:35:48Z</dc:date>
    </item>
    <item>
      <title>Re: Number of observation in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-in-dataset/m-p/839344#M331887</link>
      <description>&lt;P&gt;Update:&amp;nbsp; I think the below will not work on CAS running multi-threaded, i.e. I think it would result in duplicate values for N.&amp;nbsp; See e.g.&amp;nbsp;&lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/casdspgm/p10ux48pz7qknzn1ux1bs48vplnx.htm#n05jzo5a7djwphn1neq15j5m50iw" target="_blank"&gt;https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/casdspgm/p10ux48pz7qknzn1ux1bs48vplnx.htm#n05jzo5a7djwphn1neq15j5m50iw&lt;/A&gt;&amp;nbsp;.&amp;nbsp; Perhaps if you added /single=yes to force it to run single-threaded it would work.&amp;nbsp; And perhaps adding /single=yes to the original code using _N_ would work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;___&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I haven't tried CAS... if you just want a counter, maybe the sum statement would be an option?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data casuser.test;
  set CASUSER.CHOIXC;
  n + 1 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Would N be unique in that case, or would the multi-threading result in there being duplicate values?&amp;nbsp; I understand that there is no intrinsic ordering, so if you ran the step multiple times, you might get different results.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Oct 2022 13:26:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-in-dataset/m-p/839344#M331887</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-10-19T13:26:18Z</dc:date>
    </item>
    <item>
      <title>Re: Number of observation in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-in-dataset/m-p/839365#M331896</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;I can see in the docu for Viya that the RETAIN statement is supported&amp;nbsp;&lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p0t2ac0tfzcgbjn112mu96hkgg9o.htm" target="_blank" rel="noopener"&gt;https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p0t2ac0tfzcgbjn112mu96hkgg9o.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Really curious if that will cause CAS to execute in a single thread or if it means the data step will execute in SPRE.&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp;Do you know?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Oct 2022 12:55:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-in-dataset/m-p/839365#M331896</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-10-19T12:55:15Z</dc:date>
    </item>
    <item>
      <title>Re: Number of observation in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-in-dataset/m-p/839378#M331899</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;.&amp;nbsp; I just found this example in the docs:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/casdspgm/p10ux48pz7qknzn1ux1bs48vplnx.htm#n05jzo5a7djwphn1neq15j5m50iw" target="_blank"&gt;https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/casdspgm/p10ux48pz7qknzn1ux1bs48vplnx.htm#n05jzo5a7djwphn1neq15j5m50iw&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Which I think answers your question and mine:&lt;/P&gt;
&lt;UL class="xisDoc-listUnordered"&gt;
&lt;LI class="xisDoc-item"&gt;When you use a retained variable to hold a sum in a multithreaded DATA step, the variable contains only the sum of the values that the processing thread can see.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;In order to sum a variable across an entire dataset, they use retain in a first data step that runs multithreaded, and then run a second data step and force it to be single-threaded.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I haven't thought much about Viya, since I don't have it.&amp;nbsp; But I guess if you're used to thinking about the data step having a PDV, when it's multi-threaded it sounds like each thread has it's own PDV.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So in the first step of the example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mycas.sums;                     
  retain homeowners_sum;                 /* 1 */
  keep homeowners_sum;                   /* 2 */
  set mycas.purchase end = done;         /* 3 */
  if demog_ho = 1 then                          
     homeowners_sum + 1;                 /* 4 */
  if done then output;                   /* 5 */
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If this were running on SAS 9 or on Viya single-threaded, it would output one record.&amp;nbsp; But on CAS it looks like it outputs one record per thread.&amp;nbsp; Because each thread will have its own end= flag in its PDV. I guess. : )&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My understanding is when you submit code on Viya, Viya will decide whether a step will run on CAS or SPRE/Compute Server.&amp;nbsp; Kind of like implicit pass-through deciding whether to run a query in SAS or remote database.&amp;nbsp; But I generally don't like guessing where my code will run.&amp;nbsp; Do you know if there is a system option I can run in a Viya session that will say "please run all this code on SPRE/Compute Server"?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm looking forward to upcoming Ask the Expert on converting SAS 9 code to Viya, hopefully it will address some of these differences.&amp;nbsp;&amp;nbsp;&lt;A href="https://communities.sas.com/t5/Upcoming-Events/How-Do-I-Modify-SAS-9-Programs-to-Run-in-SAS-Viya/ec-p/838684#M355" target="_blank"&gt;https://communities.sas.com/t5/Upcoming-Events/How-Do-I-Modify-SAS-9-Programs-to-Run-in-SAS-Viya/ec-p/838684#M355&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Oct 2022 13:23:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-in-dataset/m-p/839378#M331899</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-10-19T13:23:42Z</dc:date>
    </item>
  </channel>
</rss>

