<?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: MONOTONIC() in SQL JOIN in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621829#M182864</link>
    <description>&lt;P&gt;Yes. I should have asked MONOTONIC() rather than ORDER BY MONOTONIC(). And essentially I was asking the order that PROC SQL follows to read the observations, which is random.&lt;/P&gt;&lt;P&gt;I was confused because I thought that MONOTONIC() is something readily available&amp;nbsp;before SQL joins multiple data sets hopefully—such as &lt;EM&gt;a&lt;/EM&gt;.MONOTONIC() and &lt;EM&gt;b&lt;/EM&gt;.MONOTONIC()—but actually SQL assigns the values for MONOTONIC() after joining processes, which also differs from how _N_ behaves.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
do i=1 to 100;
x=rannor(1);
output;
end;
run;
data bc;
set a;
if i&amp;gt;50;
b=_n_;
c=monotonic();
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I considered both _N_ and MONOTONIC() as something similar to ROW() in Excel—and my understanding was (1) SQL processes observations in a random order and (2) ORDER BY MONOTONIC() enables one to preserve the original order, which was why I put the nonsensical statement—but it seems I need to be careful when the order matters.&lt;/P&gt;</description>
    <pubDate>Mon, 03 Feb 2020 03:38:47 GMT</pubDate>
    <dc:creator>Junyong</dc:creator>
    <dc:date>2020-02-03T03:38:47Z</dc:date>
    <item>
      <title>MONOTONIC() in SQL JOIN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621539#M182707</link>
      <description>&lt;P&gt;Is it valid to use MONOTONIC() during JOIN in SQL? Suppose two following data sets.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
input a @@;
b=rannor(1);
cards;
1 2 3 4
;
run;
data b;
input a @@;
c=rannor(1);
cards;
5 3 2 1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here the data set&amp;nbsp;&lt;EM&gt;a&lt;/EM&gt; is ascending in &lt;EM&gt;a&lt;/EM&gt; but the data set&amp;nbsp;&lt;EM&gt;b&lt;/EM&gt; is descending in &lt;EM&gt;a&lt;/EM&gt;. The following code performs full join in SQL.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table c as select coalesce(a.a,b.a) as a,b,c from a full join b on a.a=b.a order by monotonic();
	create table d as select coalesce(a.a,b.a) as a,b,c from b full join a on a.a=b.a order by monotonic();
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The only difference is the order of FROM FULL JOIN. I thought that SQL will keep the order of the data set &lt;EM&gt;b&lt;/EM&gt; in the second case as located first, but both produces data sets &lt;EM&gt;c&lt;/EM&gt; and &lt;EM&gt;d&lt;/EM&gt;, respectively, and both data sets are sorted in an ascending order. Which order does MONOTONIC() use when accompanied with JOIN then? Must this be avoided? Thanks.&lt;/P&gt;&lt;P&gt;P.S. I also found that SQL keeps the descending order of &lt;EM&gt;b&lt;/EM&gt; when the second SQL statement uses just JOIN rather than FULL JOIN.&lt;/P&gt;</description>
      <pubDate>Fri, 31 Jan 2020 20:24:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621539#M182707</guid>
      <dc:creator>Junyong</dc:creator>
      <dc:date>2020-01-31T20:24:42Z</dc:date>
    </item>
    <item>
      <title>Re: MONOTONIC() in SQL JOIN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621589#M182736</link>
      <description>&lt;P&gt;What are you trying to do? Why would you think that ordering by the sequential values 1,2,3,4 would change the order in any way?&lt;/P&gt;
&lt;P&gt;You basically told SAS to run this query:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
title 'c';
select monotonic() as col1,* 
  from (select coalesce(a.a,b.a) as a,b,c from a full join b on a.a=b.a)
  order by col1
;
title 'd';
select monotonic() as col1,* 
  from (select coalesce(a.a,b.a) as a,b,c from b full join a on a.a=b.a)
  order by col1
;
quit;
title;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Feb 2020 02:56:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621589#M182736</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-01T02:56:49Z</dc:date>
    </item>
    <item>
      <title>Re: MONOTONIC() in SQL JOIN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621602#M182742</link>
      <description>&lt;P&gt;What is MONOTONIC() doing here then? When it comes to JOIN rather than FULL JOIN, SQL displays the opposite order as follows.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table c as select coalesce(a.a,b.a) as a,b,c from a join b on a.a=b.a order by monotonic();
	create table d as select coalesce(a.a,b.a) as a,b,c from b join a on a.a=b.a order by monotonic();
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The order of the data set&amp;nbsp;&lt;EM&gt;c&lt;/EM&gt; is &lt;EM&gt;a&lt;/EM&gt;=1,2,3, but that of the data set &lt;EM&gt;d&lt;/EM&gt; is 3,2,1 because the data set &lt;EM&gt;b&lt;/EM&gt; is in a descending order. Since MONOTONIC() assigns _n_ to each observation, shouldn't the data set &lt;EM&gt;d&lt;/EM&gt; keep the original order &lt;EM&gt;a&lt;/EM&gt;=5,3,2,1 of the data set &lt;EM&gt;b&lt;/EM&gt;?&lt;/P&gt;</description>
      <pubDate>Sat, 01 Feb 2020 06:15:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621602#M182742</guid>
      <dc:creator>Junyong</dc:creator>
      <dc:date>2020-02-01T06:15:18Z</dc:date>
    </item>
    <item>
      <title>Re: MONOTONIC() in SQL JOIN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621648#M182774</link>
      <description>&lt;P&gt;The SQL language was purposely built to treat data as logical SETS. There is no concept of an order of the elements of a set. The process that is executing the SQL query is free to return the results in any order. That is why you can only add an ORDER BY clause to the outer query.&amp;nbsp; It is ordering the observations in the result set AFTER the set has been generated.&amp;nbsp; So using MONOTONIC in an ORDER BY clause will have no impact on changing the order since the first observation it looks at will get 1 the second on 2 etc.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you care about the order that the observations appear in the input dataset then either use data step code which will process the observations one by one or add you own sequential variable to your dataset so that you can use that variable in your ORDER BY clause.&lt;/P&gt;</description>
      <pubDate>Sat, 01 Feb 2020 15:55:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621648#M182774</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-01T15:55:43Z</dc:date>
    </item>
    <item>
      <title>Re: MONOTONIC() in SQL JOIN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621702#M182807</link>
      <description>&lt;P&gt;I know SQL does random access. I only wondered what MONOTONIC() does when a query incorporates multiple data sets. It seems MONOTONIC() is unstable in these situations and only reasonable when a query with only one data set. Thanks.&lt;/P&gt;</description>
      <pubDate>Sat, 01 Feb 2020 22:06:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621702#M182807</guid>
      <dc:creator>Junyong</dc:creator>
      <dc:date>2020-02-01T22:06:34Z</dc:date>
    </item>
    <item>
      <title>Re: MONOTONIC() in SQL JOIN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621704#M182808</link>
      <description>All is does is return a value one larger than what it returned the last time it executed.  It is pretty deterministic in the queries you posted since it returns 1 for the first observation and 2 for the second, etc. So sorting by the value it returns means that the order is not changed at all.&lt;BR /&gt;The reason it might seem pseudo random in more complex queries is because you don't know how SAS is going convert your SQL set logic into actual executable code. So where the function really gets called you cannot control.&lt;BR /&gt;</description>
      <pubDate>Sat, 01 Feb 2020 22:18:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621704#M182808</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-01T22:18:12Z</dc:date>
    </item>
    <item>
      <title>Re: MONOTONIC() in SQL JOIN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621709#M182810</link>
      <description>&lt;P&gt;Here are the data sets &lt;EM&gt;a&lt;/EM&gt;, &lt;EM&gt;b&lt;/EM&gt;, &lt;EM&gt;c&lt;/EM&gt;, and &lt;EM&gt;d&lt;/EM&gt; in a clockwise order.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="monotonic.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/35802i2D721FC226C5CF40/image-size/large?v=v2&amp;amp;px=999" role="button" title="monotonic.png" alt="monotonic.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;And here are the queries.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table c as
	select coalesce(a.a,b.a) as a,b,c
	FROM A FULL JOIN B
	on a.a=b.a
	order by monotonic();
	create table d as
	select coalesce(a.a,b.a) as a,b,c
	FROM B FULL JOIN A
	on a.a=b.a
	order by monotonic();
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;There is no problem in the first query. The first observation for this query is the first observation of the data set &lt;EM&gt;a&lt;/EM&gt;—i.e. (1,1.8048229506) because the FULL JOIN uses A first and then B.&lt;/P&gt;&lt;P&gt;The problem is the second query. The FULL JOIN in the second query uses B first and then A, so the first observation for this second query should be the first observation of the data set &lt;EM&gt;b&lt;/EM&gt;—i.e. (5,1.8048229506). However, SQL put the last observation of the data set b (1,-1.083317655) as the first observation despite the ORDER BY MONOTONIC() at the end.&lt;/P&gt;&lt;P&gt;With the data set &lt;EM&gt;b&lt;/EM&gt; only, the MONOTONIC() assigns 1 for (5,1.8048), 2 for (3,-0.0799), 3 for (2,0.3966), and 4 for (1,-1.0833), and the outer ORDER BY sorts the observations in this order. Why did SAS flip this order when the data set &lt;EM&gt;a&lt;/EM&gt; is incorporated through FULL JOIN? This doesn't happen when not FULL JOIN but just JOIN—in this case, MONOTONIC() assigns 1 for (3,-0.0799), 2 for (2,0.3966), and 3 for (1,-1.0833) orderly, so the resulting data set respects their order as well.&lt;/P&gt;</description>
      <pubDate>Sat, 01 Feb 2020 23:15:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621709#M182810</guid>
      <dc:creator>Junyong</dc:creator>
      <dc:date>2020-02-01T23:15:59Z</dc:date>
    </item>
    <item>
      <title>Re: MONOTONIC() in SQL JOIN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621716#M182813</link>
      <description>&lt;P&gt;You do you NOT KNOW how SAS is going to process the data.&amp;nbsp; The only thing that is required to of an SQL implementation is that it returns the LOGICAL combination of the data the set logic in your SQL query describes.&amp;nbsp; SAS can take any route that it wants to satisgy that query as long as the result matches what you requested. Changing the order that you reference the datasets in the FROM clause does not impose any restrictions on how SAS decides to implement your query.&amp;nbsp; If it wants to it can read the dataset from back to front. Or read every even observation first and then read the odd observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to control the order out based on the order in the original dataset then add a variable into the original dataset that can be used to order the data coming out of the query.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
  a_row +1;
  input a @@;
  b=rannor(1);
cards;
1 2 3 4
;
data b;
  b_row+1;
  input a @@;
  c=rannor(1);
cards;
5 3 2 1
;

proc sql ;
title 'Order by A_ROW';
select * from a full join b on a.a=b.a 
order by a_row
;
title 'Order by B_ROW';
select * from a full join b on a.a=b.a 
order by b_row
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Order by A_ROW 

   a_row         a         b     b_row         a         c
----------------------------------------------------------
       .         .         .         1         5  1.804823
       1         1  1.804823         4         1  -1.08332
       2         2  -0.07992         3         2  0.396577
       3         3  0.396577         2         3  -0.07992
       4         4  -1.08332         .         .         .

Order by B_ROW

   a_row         a         b     b_row         a         c
----------------------------------------------------------
       4         4  -1.08332         .         .         .
       .         .         .         1         5  1.804823
       3         3  0.396577         2         3  -0.07992
       2         2  -0.07992         3         2  0.396577
       1         1  1.804823         4         1  -1.08332&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Feb 2020 01:44:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621716#M182813</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-02T01:44:21Z</dc:date>
    </item>
    <item>
      <title>Re: MONOTONIC() in SQL JOIN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621730#M182820</link>
      <description>&lt;P&gt;"Changing the order that you reference the datasets in the FROM clause does not impose any restrictions on how SAS decides to implement your query." Changing the order affects the results if not FULL JOIN but just JOIN. Let me reuse your code.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
/*  a_row +1;*/
  input a @@;
  b=rannor(1);
cards;
1 2 3 4
;
data b;
/*  b_row+1;*/
  input a @@;
  c=rannor(1);
cards;
5 3 2 1
;

proc sql ;
/*title 'Order by A_ROW';*/ title 'A JOIN B';
select * from a join b on a.a=b.a 
order by monotonic()
;
/*title 'Order by B_ROW';*/ title 'B JOIN A';
select * from b join a on a.a=b.a 
order by monotonic()
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Everything but the order is identical. Here are the results. They show the opposite sorts. What I want to know is why MONOTONIC() with FULL JOIN and MONOTONIC() with just JOIN behave in a different way. I know I can achieve the results by adding two DATA steps and going back and forth, but I want to understand what MONOTONIC() does exactly and save code lines if possible.&lt;/P&gt;&lt;PRE&gt;                                            A JOIN B

                                    a         b         a         c
                             
                                    1  1.804823         1  -1.08332
                                    2  -0.07992         2  0.396577
                                    3  0.396577         3  -0.07992

                                            B JOIN A

                                    a         c         a         b
                             
                                    3  -0.07992         3  0.396577
                                    2  0.396577         2  -0.07992
                                    1  -1.08332         1  1.804823&lt;/PRE&gt;&lt;P&gt;As stated &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/MONOTONIC-function-in-PROC-SQL/ta-p/475752" target="_blank" rel="noopener"&gt;here&lt;/A&gt;, MONOTONIC() is an undocumented function and the way it assigns each number to each observation is not stated when it comes to aggregating multiple data sets—"&lt;SPAN&gt;If the MONOTONIC function is used in an&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;SQL procedure&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;that aggregates data then the function may return non-sequential or missing results.&lt;/SPAN&gt;"&lt;/P&gt;</description>
      <pubDate>Sun, 02 Feb 2020 06:16:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621730#M182820</guid>
      <dc:creator>Junyong</dc:creator>
      <dc:date>2020-02-02T06:16:36Z</dc:date>
    </item>
    <item>
      <title>Re: MONOTONIC() in SQL JOIN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621784#M182843</link>
      <description>&lt;P&gt;You don't seem to be hearing what I am saying. Why would you think that asking SAS to calculate a sequence number while it is in the final step of delivering the data cause the order to change?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Run your same queries with and without the ORDER BY MONONOTIC() clause and you will notice that the results are not changed by adding the order by clause.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SAS's implementation TODAY of your two different ways of asking for the same information is returning the results for in different order.&amp;nbsp; But they could make a change to how they implement the query in some future release (or in SAS running on a different operating system) and the results could come out in same the same order for both or each could be in some other order and it would not mean that SAS was "broken".&amp;nbsp; There is no contract between you can the SQL implementation to return the values in any particular order.&lt;/P&gt;</description>
      <pubDate>Sun, 02 Feb 2020 16:54:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621784#M182843</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-02T16:54:05Z</dc:date>
    </item>
    <item>
      <title>Re: MONOTONIC() in SQL JOIN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621808#M182852</link>
      <description>&lt;P&gt;I didn't say SAS was "broken." I was more asking the function of MONOTONIC() rather than ORDER BY MONOTONIC(), but it seems I need to more pinpoint my curiosity down.&amp;nbsp;According to your replies,&lt;/P&gt;&lt;P&gt;1. MONOTONIC() is the function that gives 1 to the first observation it processes, 2 to the second observation it processes, and so forth.&lt;/P&gt;&lt;P&gt;2. The order that SQL follows to read observations is completely unknown.&lt;/P&gt;&lt;P&gt;3. Therefore, the resulting value that MONOTONIC() assigns to each observation is unpredictable in SQL.&lt;/P&gt;&lt;P&gt;If these three are correct then,&lt;/P&gt;&lt;P&gt;a. In a DATA step, MONOTONIC() returns orderly values to the observations.&lt;/P&gt;&lt;P&gt;b. In a SQL query, MONOTONIC() doesn't necessarily return orderly values to the observations—even in a query with only one data, the orderly values for the observations are not guaranteed.&lt;/P&gt;&lt;P&gt;c. In a DATA step, just adding an _N_ column is identical to just adding a MONOTONIC() column.&lt;/P&gt;&lt;P&gt;d. Just adding a MONOTONIC() column in a DATA step and the same thing in a SQL query are not guaranteed to be identical.&lt;/P&gt;&lt;P&gt;Am I correct?&lt;/P&gt;</description>
      <pubDate>Sun, 02 Feb 2020 20:37:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621808#M182852</guid>
      <dc:creator>Junyong</dc:creator>
      <dc:date>2020-02-02T20:37:35Z</dc:date>
    </item>
    <item>
      <title>Re: MONOTONIC() in SQL JOIN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621820#M182855</link>
      <description>&lt;P&gt;When you are saying ORDER BY MONOTONIC() it is essentially saying "sort the data in the output dataset by the order that it is already in" .&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think you are getting confused , and assuming that it would sort the data by the order that the input dataset is in .&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Feb 2020 23:46:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621820#M182855</guid>
      <dc:creator>fdsaaaa</dc:creator>
      <dc:date>2020-02-02T23:46:50Z</dc:date>
    </item>
    <item>
      <title>Re: MONOTONIC() in SQL JOIN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621829#M182864</link>
      <description>&lt;P&gt;Yes. I should have asked MONOTONIC() rather than ORDER BY MONOTONIC(). And essentially I was asking the order that PROC SQL follows to read the observations, which is random.&lt;/P&gt;&lt;P&gt;I was confused because I thought that MONOTONIC() is something readily available&amp;nbsp;before SQL joins multiple data sets hopefully—such as &lt;EM&gt;a&lt;/EM&gt;.MONOTONIC() and &lt;EM&gt;b&lt;/EM&gt;.MONOTONIC()—but actually SQL assigns the values for MONOTONIC() after joining processes, which also differs from how _N_ behaves.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
do i=1 to 100;
x=rannor(1);
output;
end;
run;
data bc;
set a;
if i&amp;gt;50;
b=_n_;
c=monotonic();
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I considered both _N_ and MONOTONIC() as something similar to ROW() in Excel—and my understanding was (1) SQL processes observations in a random order and (2) ORDER BY MONOTONIC() enables one to preserve the original order, which was why I put the nonsensical statement—but it seems I need to be careful when the order matters.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Feb 2020 03:38:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621829#M182864</guid>
      <dc:creator>Junyong</dc:creator>
      <dc:date>2020-02-03T03:38:47Z</dc:date>
    </item>
    <item>
      <title>Re: MONOTONIC() in SQL JOIN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621847#M182877</link>
      <description>&lt;P&gt;The monotonic() function (or any other function really) has nothing to do with how SAS assigns values to the _N_ automatic variable in a data step.&amp;nbsp; Also the _N_ automatic variable has nothing to do with observations. It is a just count of the number of times the data step has iterated.&amp;nbsp; Check the values output to the log for _N_&amp;nbsp; from these two ways to make a copy of SASHELP.CLASS.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1;
  put _n_=;
  set sashelp.class;
run;
data test2;
  do while (1=1);
    put _n_=;
    set sashelp.class;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You cannot have any confidence in what values MONOTONIC() will return in an SQL query because you don't have any confidence in what order SAS will do in processing the query.&amp;nbsp; It is free to optimize the query in any way that it wants.&amp;nbsp; &amp;nbsp;Since monotonic is undocumented you are not even sure it will count form 1 instead of starting somewhere else.&lt;/P&gt;
&lt;P&gt;When I ran these queries the last result gererates 6 to 10 instead of 1 to 5.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
title 'No Order - a full join b';
select monotonic() as Ord,* from a full join b on a.a=b.a;
title 'No Order - b full join a';
select monotonic() as Ord,* from b full join a on a.a=b.a;
title 'Order by A_ROW,B_ROW';
select monotonic() as Ord,* from a full join b on a.a=b.a
order by a_row,b_row
;
title 'Order by B_ROW,A_ROW';
select monotonic() as Ord,* from b full join a on a.a=b.a
order by b_row,a_row
;
quit;
title;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Feb 2020 03:40:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/MONOTONIC-in-SQL-JOIN/m-p/621847#M182877</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-03T03:40:23Z</dc:date>
    </item>
  </channel>
</rss>

