<?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: Duplicate observations with new StateID column? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Duplicate-observations-with-new-StateID-column/m-p/577979#M163867</link>
    <description>&lt;P&gt;Simple do loop, and sorting afterwards:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
do StateID = 1 to 13;
  output;
end;
run;

proc sort data=want;
by StateID Id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 31 Jul 2019 08:07:55 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-07-31T08:07:55Z</dc:date>
    <item>
      <title>Duplicate observations with new StateID column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicate-observations-with-new-StateID-column/m-p/577974#M163863</link>
      <description>&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; have&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;input&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;Id&lt;/SPAN&gt; Twin Age1 Age2 Sex1 &lt;SPAN class="token punctuation"&gt;$&lt;/SPAN&gt; Sex2 &lt;SPAN class="token punctuation"&gt;$ Cost&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token statement"&gt;infile&lt;/SPAN&gt; datalines missover&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token keyword"&gt;datalines&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;SPAN class="token data string"&gt;1 1 45 35 M F 10
2 0 32 .  F  20
3 0 26 .  M  30
4 1 20 21 M F .&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;With the dataset above, is there a way for me to duplicate the record set with a new column called "StateID" with every 1 full set having 1 StateID. For example, first full set of data will have StateId=1. 2nd set onwards will have Stateid=2 and all the way until the 13th set which will have StateId=13.&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 full set = 34 rows.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can i achieve that?&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jul 2019 07:46:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicate-observations-with-new-StateID-column/m-p/577974#M163863</guid>
      <dc:creator>imdickson</dc:creator>
      <dc:date>2019-07-31T07:46:45Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicate observations with new StateID column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicate-observations-with-new-StateID-column/m-p/577975#M163864</link>
      <description>&lt;P&gt;So you have a dataset with 34 * 13 observations, and you want to increment your new column every 34 observations?&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jul 2019 07:49:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicate-observations-with-new-StateID-column/m-p/577975#M163864</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-31T07:49:45Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicate observations with new StateID column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicate-observations-with-new-StateID-column/m-p/577976#M163865</link>
      <description>&lt;P&gt;Meaning to say 1 full set of data has 34 rows.&lt;/P&gt;&lt;P&gt;What i want is every 34 rows of record, stateid will increase by 1.&lt;/P&gt;&lt;P&gt;So the first 34 rows will have StateID=1.&lt;/P&gt;&lt;P&gt;Second 34 rows will have StateID=2 and so forth.&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jul 2019 07:52:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicate-observations-with-new-StateID-column/m-p/577976#M163865</guid>
      <dc:creator>imdickson</dc:creator>
      <dc:date>2019-07-31T07:52:04Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicate observations with new StateID column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicate-observations-with-new-StateID-column/m-p/577977#M163866</link>
      <description>&lt;P&gt;Currently there is only 34 rows of records in this table.&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jul 2019 07:54:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicate-observations-with-new-StateID-column/m-p/577977#M163866</guid>
      <dc:creator>imdickson</dc:creator>
      <dc:date>2019-07-31T07:54:54Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicate observations with new StateID column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicate-observations-with-new-StateID-column/m-p/577979#M163867</link>
      <description>&lt;P&gt;Simple do loop, and sorting afterwards:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
do StateID = 1 to 13;
  output;
end;
run;

proc sort data=want;
by StateID Id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 31 Jul 2019 08:07:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicate-observations-with-new-StateID-column/m-p/577979#M163867</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-31T08:07:55Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicate observations with new StateID column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicate-observations-with-new-StateID-column/m-p/577985#M163870</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/63520"&gt;@imdickson&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;This is perhaps a relatively efficient way since it reads the input data set once but requires sorting if you want WANT to be sorted by StateID:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                        
  retain stateID ;                                 
  input Id Twin Age1 Age2 (Sex1 Sex2) (:$1.) Cost ;
  cards ;                                          
1 1 45 35 M F 10                                   
2 0 32  . F . 20                                   
3 0 26  . M . 30                                   
4 1 20 21 M F  .                                   
;                                                  
run ;                                              
                                                   
data want ;                                        
  retain stateID ;                                 
  set have ;                                       
  do stateID = 1 to 13 ;                           
    output ;                                       
  end ;                                            
run ;                                              
                                                   
proc sort ;                                        
  by stateID ;                                     
run ;                                              
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This is a much less efficient way since you have to re-read the input data set 12 times; but you don't have to sort:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;                        
  do StateID = 1 to 13 ;           
    do p = 1 to n ;                
      set have point = p nobs = n ;
      output ;                     
    end ;                          
  end ;                            
  stop ;                           
run ;                              
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Yet another way, probably most efficient since it reads HAVE only once and requires no sorting or means to put StateID first in the variable list:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data v / view = v ;                           
  do StateID = 1 to 13 ;                      
    output ;                                  
  end ;                                       
run ;                                         
                                              
proc sql ;                                    
  create table want as select * from v, have ;
quit ;                                        
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It's still most efficient, even though the log says:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;NOTE: The execution of this query involves performing one or more Cartesian product joins that can not be optimized.
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The note is valid; however, it doesn't testify to an inefficiency because the Cartesian product is exactly what here you're aiming to create.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jul 2019 08:50:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicate-observations-with-new-StateID-column/m-p/577985#M163870</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-07-31T08:50:55Z</dc:date>
    </item>
  </channel>
</rss>

