<?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: How to print variable observations alternatively in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-print-variable-observations-alternatively/m-p/588804#M168338</link>
    <description>&lt;P&gt;Thank You&lt;/P&gt;&lt;P&gt;Sharp&lt;/P&gt;</description>
    <pubDate>Sun, 15 Sep 2019 07:00:57 GMT</pubDate>
    <dc:creator>BrahmanandaRao</dc:creator>
    <dc:date>2019-09-15T07:00:57Z</dc:date>
    <item>
      <title>How to print variable observations alternatively</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-print-variable-observations-alternatively/m-p/588716#M168295</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA DSN;
INPUT SEX$;
DATALINES;
F
F
F
F
M
M
M
M
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want output&lt;/P&gt;&lt;P&gt;F&lt;/P&gt;&lt;P&gt;M&lt;/P&gt;&lt;P&gt;F&lt;/P&gt;&lt;P&gt;M&lt;/P&gt;&lt;P&gt;F&lt;/P&gt;&lt;P&gt;M&lt;/P&gt;&lt;P&gt;F&lt;/P&gt;&lt;P&gt;M&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>Sat, 14 Sep 2019 09:32:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-print-variable-observations-alternatively/m-p/588716#M168295</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2019-09-14T09:32:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to print variable observations alternatively</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-print-variable-observations-alternatively/m-p/588717#M168296</link>
      <description>&lt;P&gt;Below will work for your sample data but be aware that the SAS data step will stop as soon as one of the groups doesn't have any further observations (=if the groups are not equal some source observations will not be written to target).&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have(where=(sex='F'));
  output;
  set have(where=(sex='M'));
  output;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is that o.k. or do you need an approach with writes all the source obs to target?&lt;/P&gt;</description>
      <pubDate>Sat, 14 Sep 2019 09:38:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-print-variable-observations-alternatively/m-p/588717#M168296</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-09-14T09:38:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to print variable observations alternatively</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-print-variable-observations-alternatively/m-p/588720#M168298</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA DSN;
INPUT SEX$;
DATALINES;
F
F
F
F
M
M
M
M
;
RUN;
data F;
  set dsn(where=(sex='F'));
  n+1;
run;
data M;
  set dsn(where=(sex='M'));
  n+1;
run;
data want; 
 set F M;
 by n;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 14 Sep 2019 10:34:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-print-variable-observations-alternatively/m-p/588720#M168298</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-09-14T10:34:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to print variable observations alternatively</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-print-variable-observations-alternatively/m-p/588724#M168301</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

DATA DSN;
INPUT SEX$;
DATALINES;
F
F
F
F
M
M
M
M
;
RUN;


data _null_;
if _n_=1 then do;
   dcl hash H (ordered: "A",multidata:'y') ;
   h.definekey  ("_n_") ;
   h.definedata ("sex") ;
   h.definedone () ;
end;
do _n_=1 by 1 until(last.sex);
 set dsn end=z;
 by sex;
 h.add();
 output;
end;
if z ;
h.output(dataset:'want');
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 14 Sep 2019 12:31:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-print-variable-observations-alternatively/m-p/588724#M168301</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-09-14T12:31:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to print variable observations alternatively</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-print-variable-observations-alternatively/m-p/588726#M168302</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; When you say "print" do you mean that you need a report? By making a "helper" variable to control ordering, you can create the data, then order as you want. PROC REPORT allows you to use the "helper" variable but hide it on the report, as shown in #1. Report #2 reveals the ROWORD helper variable value to control ordering:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="roword.png" style="width: 444px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/32467i9F4AF03182D9A0B0/image-size/large?v=v2&amp;amp;px=999" role="button" title="roword.png" alt="roword.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I added NAME to the fake data, so you could see that the reordering was done correctly when ROWORD was hidden.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The program is here:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA fakedata;
INPUT name $ SEX $;
DATALINES;
Anna F
Barb F
Cindy F
Diane F
Art M
Bob M
Carl M
Denis M
;
RUN;

data final;
  set fakedata;
  by sex;
  if first.sex then roword=0;
  roword+1;
run;

proc report data=final;
title '1) Use Helper Variable ROWORD to reorder report rows';
  column roword name sex;
  define roword / order noprint;
  define name / display;
  define sex / display;
run;
title;


proc report data=final;
title '2) See value of Helper Variable ROWORD';
  column roword name sex;
  define roword / order;
  define name / display;
  define sex / display;
run;
title;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps provide an alternative approach.&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;</description>
      <pubDate>Sat, 14 Sep 2019 14:02:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-print-variable-observations-alternatively/m-p/588726#M168302</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2019-09-14T14:02:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to print variable observations alternatively</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-print-variable-observations-alternatively/m-p/588728#M168303</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265860"&gt;@BrahmanandaRao&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's another approach for an input dataset consisting of two blocks of equal size (irrespective of variable values):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do _n_=1 to n;
  p=ifn(_n_=1,1,p+n/2-(n-1)*mod(_n_,2));
  set dsn nobs=n point=p;
  output;
end;
stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Feel free to use a suitable PUT statement (e.g. in conjunction with FILE PRINT in a DATA _NULL_ step) rather than OUTPUT in order to create printed output.&lt;/P&gt;</description>
      <pubDate>Sat, 14 Sep 2019 14:18:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-print-variable-observations-alternatively/m-p/588728#M168303</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-09-14T14:18:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to print variable observations alternatively</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-print-variable-observations-alternatively/m-p/588733#M168304</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA DSN;
INPUT SEX$;
DATALINES;
F
F
F
F
M
M
M
M
;
RUN;


data want;
 dcl hash H (dataset:'dsn',ordered:'y') ;
 h.definekey  ("sex") ;
 h.definedata ("sex") ;
 h.definedone () ;
 dcl hiter hi('h');
do _n_=1 to n/2;
 do while(hi.next()=0);
  output;
 end;
end;
stop;
set dsn nobs=n;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 14 Sep 2019 15:52:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-print-variable-observations-alternatively/m-p/588733#M168304</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-09-14T15:52:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to print variable observations alternatively</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-print-variable-observations-alternatively/m-p/588780#M168326</link>
      <description>&lt;P&gt;And just for fun as a total overkill below a hash of hashes approach.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code is largely based on what&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp; shared here:&amp;nbsp;&lt;A href="http://sasnrd.com/sas-hash-object-of-hash/" target="_blank" rel="noopener"&gt;http://sasnrd.com/sas-hash-object-of-hash/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;The source data doesn't need to be pre-sorted nor is it necessary to know the number of distinct values for sex in advance.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input sex $;
  datalines;
f
f
f
f
m
m
x
m
m
x
;

data want(drop=_:);
  declare hash HoH();
  HoH.definekey ('sex');
  HoH.definedata('h','sex');
  HoH.definedone();
  declare hiter HoHiter("HoH");
  declare hash h;

  do until (eof);
    set have end=eof;

    if HoH.find() ne 0 then
      do;
        h=_new_ hash(dataset:'have(obs=0)', multidata:'Y');
        h.definekey('sex');
        h.definedata(all:'Y');
        h.definedone();
        HoH.add();
      end;

    h.add();
  end;

  do until(_itemsLeft=0);
    _itemsLeft=0;
    do while(HoHiter.next() = 0);
      if h.num_items&amp;gt;0 then
        do;
          h.find();
          h.removedup();
          if h.num_items&amp;gt;0 then _itemsLeft=1;
          output;
        end;
    end;
  end;
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 14 Sep 2019 23:46:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-print-variable-observations-alternatively/m-p/588780#M168326</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-09-14T23:46:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to print variable observations alternatively</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-print-variable-observations-alternatively/m-p/588803#M168337</link>
      <description>&lt;P&gt;Thank you&lt;/P&gt;&lt;P&gt;Patrick&lt;/P&gt;</description>
      <pubDate>Sun, 15 Sep 2019 07:00:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-print-variable-observations-alternatively/m-p/588803#M168337</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2019-09-15T07:00:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to print variable observations alternatively</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-print-variable-observations-alternatively/m-p/588804#M168338</link>
      <description>&lt;P&gt;Thank You&lt;/P&gt;&lt;P&gt;Sharp&lt;/P&gt;</description>
      <pubDate>Sun, 15 Sep 2019 07:00:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-print-variable-observations-alternatively/m-p/588804#M168338</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2019-09-15T07:00:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to print variable observations alternatively</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-print-variable-observations-alternatively/m-p/588848#M168351</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;Love your p-arithmetic! &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&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>Sun, 15 Sep 2019 16:41:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-print-variable-observations-alternatively/m-p/588848#M168351</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-09-15T16:41:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to print variable observations alternatively</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-print-variable-observations-alternatively/m-p/588851#M168352</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;Another way in a somewhat similar vein:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                                   
  input (name sex) ($) ;                                                      
  cards ;                                                                     
Alice  F                                                                      
Beth   F                                                                      
Carol  F                                                                      
Doris  F                                                                      
Alex   M                                                                      
Bob    M                                                                      
Cary   M                                                                      
Doug   M                                                                      
;                                                                             
run ;                                                                         
                                                                              
data want (drop = _:) ;                                                       
  merge have (where=(sex="F")) have (where=(_s="M") rename=(name=_n sex=_s)) ;
  output ;                                                                    
  name = _n ;                                                                 
  sex  = _s ;                                                                 
  output ;                                                                    
run ;                                                                         
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Output-wise, it's a little different from your double SET in that if some F or M has no counterpart, her/his record will be where it would be if the counterpart existed but with missing values.&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; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 15 Sep 2019 16:51:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-print-variable-observations-alternatively/m-p/588851#M168352</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-09-15T16:51:43Z</dc:date>
    </item>
  </channel>
</rss>

