<?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: Create a table with increasing integers from 1 to N in SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918626#M361846</link>
    <description>&lt;P&gt;I would think PROC EXPAND could fill out a series for you with a constant value.&amp;nbsp; Perhaps you just did not find the right option for how to specify the constant value you wanted it to use?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But don't bother trying to make SQL do something like iterating.&amp;nbsp; That is not part of the grammar that SQL uses to describe the actions it can take.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to make sure your data always has observations for ID=1 to 10 then do something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data skeleton;
  do id=1 to 10;
    output;
  end;
run;
data want;
  merge skeleton actual_data;
  by id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;No SQL needed (or wanted).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 01 Mar 2024 16:59:22 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-03-01T16:59:22Z</dc:date>
    <item>
      <title>Create a table with increasing integers from 1 to N in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918442#M361778</link>
      <description>&lt;P&gt;I'm trying to "translate" this data step to SQL&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%let N = 1000;&lt;BR /&gt;data want;
	do i=1 to &amp;amp;N;
		output; end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;which simply creates such a table&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Rabelais_0-1709226229549.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/94261i87454B4D32C10B8B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Rabelais_0-1709226229549.png" alt="Rabelais_0-1709226229549.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I tried many codes using functions from other platform/languages but none of them work, for example&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/*SNOWFLAKE*/
proc sql;
	select seq4() as number
	from table(generator(rowcount =&amp;gt; &amp;amp;N))
	order by 1;
quit;

/*SQL SERVER*/
proc sql;
	SELECT * FROM generate_series(0,&amp;amp;N);
quit;

proc sql;
	SELECT * FROM UNNEST(GENERATE_ARRAY(0, &amp;amp;N));
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas? Is there a similar function in SAS?&lt;/P&gt;</description>
      <pubDate>Thu, 29 Feb 2024 17:36:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918442#M361778</guid>
      <dc:creator>Rabelais</dc:creator>
      <dc:date>2024-02-29T17:36:01Z</dc:date>
    </item>
    <item>
      <title>Re: Create a table with increasing integers from 1 to N in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918445#M361781</link>
      <description>&lt;P&gt;Look up the monotonic() function in proc sql.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Feb 2024 17:28:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918445#M361781</guid>
      <dc:creator>ChanceTGardener</dc:creator>
      <dc:date>2024-02-29T17:28:08Z</dc:date>
    </item>
    <item>
      <title>Re: Create a table with increasing integers from 1 to N in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918446#M361782</link>
      <description>&lt;P&gt;I meant to include the example code below in my last post.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
 create table myair as 
  select *
  ,	     monotonic() as rowidx
  from sashelp.air;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 29 Feb 2024 17:31:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918446#M361782</guid>
      <dc:creator>ChanceTGardener</dc:creator>
      <dc:date>2024-02-29T17:31:35Z</dc:date>
    </item>
    <item>
      <title>Re: Create a table with increasing integers from 1 to N in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918448#M361784</link>
      <description>&lt;P&gt;In general SQL does not support looping structures like DO i= . It also generally does not process records in anything resembling a specific order. So why is this supposed to be done in SQL?&lt;/P&gt;
&lt;P&gt;If this just a learning exercise the take away is that this sort of activity is not an SQL strong point.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Feb 2024 17:36:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918448#M361784</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-02-29T17:36:50Z</dc:date>
    </item>
    <item>
      <title>Re: Create a table with increasing integers from 1 to N in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918449#M361785</link>
      <description>&lt;P&gt;I see that you use an input table and count the rows. But what if N is 10000, 10000000 or bigger? Isn't possible to create such a table without using an input table?&lt;/P&gt;</description>
      <pubDate>Thu, 29 Feb 2024 17:39:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918449#M361785</guid>
      <dc:creator>Rabelais</dc:creator>
      <dc:date>2024-02-29T17:39:12Z</dc:date>
    </item>
    <item>
      <title>Re: Create a table with increasing integers from 1 to N in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918451#M361787</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/446135"&gt;@Rabelais&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think in PROC SQL you might use the undocumented MONOTONIC function -- as&amp;nbsp;&lt;A class="trigger-hovercard" href="https://communities.sas.com/t5/user/viewprofilepage/user-id/218062" target="_blank"&gt;ChanceTGardener&lt;/A&gt;&amp;nbsp;has already suggested -- and so did I (reluctantly) when someone asked a similar question in 2019 (see&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Programming/DO-Loop-with-OUTPUT-in-SQL/m-p/559237/highlight/true#M156137" target="_blank" rel="noopener"&gt;Re: DO Loop with OUTPUT in SQL?&lt;/A&gt;). So, if you have a dataset at hand with at least N observations (e.g., the 428 obs. in SASHELP.CARS for N=100), you can use something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let N=100;

proc sql;
create table want(where=(i&amp;lt;=&amp;amp;N)) as
select monotonic() as i from sashelp.cars;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Otherwise it gets more complicated as you need to create such a dataset (or view) from scratch, e.g., like view BASE below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
create table seed (x num);
insert into seed
set x=0
set x=1;

select 'seed'||repeat(',seed',int(log2(&amp;amp;N)-1)) into :ds
from seed;

create view base as
select ' ' from &amp;amp;ds;

create table want(where=(i&amp;lt;=&amp;amp;N)) as
select monotonic() as i from base;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Again, this is really not recommended. Use a DATA step instead.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Feb 2024 17:47:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918451#M361787</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-02-29T17:47:35Z</dc:date>
    </item>
    <item>
      <title>Re: Create a table with increasing integers from 1 to N in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918519#M361808</link>
      <description>&lt;P&gt;There is the undocumented (and NOT supported) monotonic() functions mentioned by others. My advice is to never use undocumented functions in a production environment.&lt;/P&gt;
&lt;P&gt;You can't do this with SAS ANSI SQL and you don't have to given it's so simple using a SAS datastep.&lt;/P&gt;
&lt;P&gt;There are SQL extensions in many databases that allow for creation of such row numbering and if you need this in a database table then you can use explicit passthrough SQL.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Mar 2024 00:20:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918519#M361808</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-03-01T00:20:41Z</dc:date>
    </item>
    <item>
      <title>Re: Create a table with increasing integers from 1 to N in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918523#M361810</link>
      <description>&lt;P&gt;This sounds like an &lt;A href="https://xyproblem.info/" target="_self"&gt;XY Problem&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why do you want make such a dataset?&lt;BR /&gt;Why don't you just use a data step since you already showed you know how to that?&lt;/P&gt;
&lt;P&gt;Why would you even want to attempt to do it in PROC SQL?&lt;/P&gt;</description>
      <pubDate>Fri, 01 Mar 2024 01:11:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918523#M361810</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-03-01T01:11:30Z</dc:date>
    </item>
    <item>
      <title>Re: Create a table with increasing integers from 1 to N in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918591#M361830</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Curiosity is the lust of the mind!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;As a new SAS user, I'm asking myself lot of questions.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Q2 &amp;amp; Q3: (1) Lot of tasks can be done by using different approaches and since I'm working with large datasets (this is not the case since generating even a big sequence is fast) I usually look for the most efficient one. (2) Since I'm new to SAS I often wonder if my code can be improved. (3) Also, sometimes&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;I'm simply curious about different ways of doing the same task.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Q1: see the toy example below (it's just 10 rows, but I'm working with millions).&lt;/P&gt;&lt;P&gt;Basically I need to "expand" the dataset HAVE so that "i" goes from 1 to N.&lt;/P&gt;&lt;P&gt;I was wondering if the second data step and the proc sql could be easily combined into a single proc sql or data step.&lt;/P&gt;&lt;P&gt;I tried using PROC EXPAND to interpolate the missing "i" values, but what it does is rather to replace a missing value with an interpolated one on an already present row, it doesn't add new rows.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;&lt;BR /&gt;input i c; cards;&lt;BR /&gt;7 0.2&lt;BR /&gt;3 0.5&lt;BR /&gt;6 0.3&lt;BR /&gt;run;

data sequence;
	do i=1 to 10;
		output;
	end;
run;

proc sql;
	create table want as select
		a.i, b.c
	from sequence a
	left join have b
	on a.i = b.i;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Rabelais_0-1709311865311.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/94306iBF6951348D547370/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Rabelais_0-1709311865311.png" alt="Rabelais_0-1709311865311.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Rabelais_1-1709311879926.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/94307iF017AF12AA7F44F7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Rabelais_1-1709311879926.png" alt="Rabelais_1-1709311879926.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;DIV class=""&gt;&lt;DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;P class=""&gt;&amp;nbsp;&lt;/P&gt;&lt;P class=""&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 01 Mar 2024 16:55:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918591#M361830</guid>
      <dc:creator>Rabelais</dc:creator>
      <dc:date>2024-03-01T16:55:00Z</dc:date>
    </item>
    <item>
      <title>Re: Create a table with increasing integers from 1 to N in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918617#M361841</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/446135"&gt;@Rabelais&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Q1: see the toy example below (it's just 10 rows, but I'm working with millions).&lt;/P&gt;
&lt;P&gt;Basically I need to "expand" the dataset HAVE so that "i" goes from 1 to N.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I strongly suggest that you show an example of what that expansion looks like. I can think of at least two possible different "expansions" but without an idea of the result should look like it isn't worth bothering to write any example code.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Mar 2024 16:43:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918617#M361841</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-03-01T16:43:43Z</dc:date>
    </item>
    <item>
      <title>Re: Create a table with increasing integers from 1 to N in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918621#M361842</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&lt;/P&gt;&lt;P&gt;I added pictures with code's output, i.e. HAVE and WANT datasets&lt;/P&gt;</description>
      <pubDate>Fri, 01 Mar 2024 16:53:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918621#M361842</guid>
      <dc:creator>Rabelais</dc:creator>
      <dc:date>2024-03-01T16:53:19Z</dc:date>
    </item>
    <item>
      <title>Re: Create a table with increasing integers from 1 to N in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918626#M361846</link>
      <description>&lt;P&gt;I would think PROC EXPAND could fill out a series for you with a constant value.&amp;nbsp; Perhaps you just did not find the right option for how to specify the constant value you wanted it to use?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But don't bother trying to make SQL do something like iterating.&amp;nbsp; That is not part of the grammar that SQL uses to describe the actions it can take.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to make sure your data always has observations for ID=1 to 10 then do something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data skeleton;
  do id=1 to 10;
    output;
  end;
run;
data want;
  merge skeleton actual_data;
  by id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;No SQL needed (or wanted).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Mar 2024 16:59:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918626#M361846</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-03-01T16:59:22Z</dc:date>
    </item>
    <item>
      <title>Re: Create a table with increasing integers from 1 to N in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918627#M361847</link>
      <description>&lt;PRE&gt;proc sort data=have;
   by i;
run;

data want;
  merge sequence
        have;
  by i;
run;&lt;/PRE&gt;
&lt;P&gt;Assuming sequence is built as described.&lt;/P&gt;
&lt;P&gt;The explicit sort and merge may run faster than SQL as SQL does that in the background for the Join on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BUT you talk about "millions" of observations. Are any of the I values in HAVE ever duplicated? If so you need to include such in your example and perhaps describe rules involved.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Mar 2024 17:18:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918627#M361847</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-03-01T17:18:06Z</dc:date>
    </item>
    <item>
      <title>Re: Create a table with increasing integers from 1 to N in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918628#M361848</link>
      <description>&lt;P&gt;SQL is designed to manage data and manipulate it, typically not to create new rows/records. Even a pivot isn't a simple operation in SQL.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SAS on the other hand is more data wrangling/analysis friendly.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Right tool for the right job.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Several different sql options are listed here you can play around with different DBs, and different dbs will have different methods.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But not SQL also has the ability to create unique key identifiers on each row automatically, which SAS does not.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Mar 2024 17:28:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918628#M361848</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2024-03-01T17:28:31Z</dc:date>
    </item>
    <item>
      <title>Re: Create a table with increasing integers from 1 to N in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918636#M361852</link>
      <description>&lt;P&gt;An alternative approach that avoids combining a DATA step and PROC SQL is to perform the merge in PROC IML (SAS's Interactive Matrix Language).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options fullstimer;
proc iml;
  N = 10;

  /* create a column vector where element 1 = 1, element 2 = 2,..., element N = N */
  i = t(1:N);

  /* read work.have into a matrix */
  use work.have;
   read all var {"i" "c"} into x;
  close work.have;

  /* populate Nx1 vector of missing values */
  c = j(N,1,.);

  /* for intersecting values in {1..N} and the first column in x (i.e., column i from the work.have data set), replace &lt;BR /&gt;    the missing value with the second column in x (i.e., column c from the work.have data set) */
  c[x[,1]] = x[,2];

  /* create output data set */
  create work.want var {i c};
   append;       
  close work.have; 
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The FULLSTIMER option lists performance metrics to the log, and is helpful when comparing multiple competing approaches. Below are the results from my desktop version.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;NOTE: The data set WORK.WANT has 10 observations and 2 variables.
NOTE: PROCEDURE IML used (Total process time):
      real time           0.02 seconds
      user cpu time       0.00 seconds
      system cpu time     0.00 seconds
      memory              582.78k
      OS Memory          21360.00k&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To your point (3) above, this is one alternative to the DATA step + SQL procedure approach that combines everything into one procedure, but it's unclear which is more efficient on your real data. The FULLSTIMER will help you test that.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Mar 2024 18:30:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918636#M361852</guid>
      <dc:creator>ChanceTGardener</dc:creator>
      <dc:date>2024-03-01T18:30:00Z</dc:date>
    </item>
    <item>
      <title>Re: Create a table with increasing integers from 1 to N in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918638#M361854</link>
      <description>&lt;P&gt;Brute force manual method:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table integers
(num int);
insert into integers values(0);
insert into integers values(1);
insert into integers values(2);
insert into integers values(3);
insert into integers values(4);
insert into integers values(5);
insert into integers values(6);
insert into integers values(7);
insert into integers values(8);
insert into integers values(9);
quit;

proc sql;
create table want as
select ones.num + tens.num*10 + hundreds.num*100 as total
from integers as ones, integers as tens, integers as hundreds
having total &amp;lt; 130
order by 1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 01 Mar 2024 18:57:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918638#M361854</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2024-03-01T18:57:10Z</dc:date>
    </item>
    <item>
      <title>Re: Create a table with increasing integers from 1 to N in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918675#M361875</link>
      <description>&lt;P&gt;I've just noticed a minor inefficiency in the code that I copied from my old post: If N happens to be a power of 2 (2, 4, 8, 16, ...), view BASE creates 2N observations, while N would be sufficient. This can be avoided by replacing&amp;nbsp;&lt;FONT face="courier new,courier"&gt;int(log2(&amp;amp;N)-1)&lt;/FONT&gt; with&amp;nbsp;&lt;FONT face="courier new,courier"&gt;ceil(log2(&amp;amp;N)-2)&lt;/FONT&gt;:&lt;/P&gt;
&lt;PRE&gt;%let N=100;

proc sql noprint;
create table seed (x num);
insert into seed
set x=0
set x=1;

select 'seed'||repeat(',seed',&lt;FONT color="#00DF00"&gt;&lt;STRONG&gt;ceil&lt;/STRONG&gt;&lt;/FONT&gt;(log2(&amp;amp;N)-&lt;FONT color="#00DF00"&gt;&lt;STRONG&gt;2&lt;/STRONG&gt;&lt;/FONT&gt;)) into :ds
from seed;

create view base as
select ' ' from &amp;amp;ds;

create table want(where=(i&amp;lt;=&amp;amp;N)) as
select monotonic() as i from base;
quit;&lt;/PRE&gt;</description>
      <pubDate>Sat, 02 Mar 2024 09:49:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-table-with-increasing-integers-from-1-to-N-in-SQL/m-p/918675#M361875</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-03-02T09:49:07Z</dc:date>
    </item>
  </channel>
</rss>

