<?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 left shift sets of variables? in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-to-left-shift-sets-of-variables/m-p/637206#M21434</link>
    <description>&lt;P&gt;Can you explain the shifting-rules in more details, please?&lt;/P&gt;
&lt;P&gt;I don't understand why b3 is missing in the second obs.&lt;/P&gt;</description>
    <pubDate>Fri, 03 Apr 2020 11:31:21 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2020-04-03T11:31:21Z</dc:date>
    <item>
      <title>How to left shift sets of variables?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-left-shift-sets-of-variables/m-p/637203#M21433</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have a dataset where I need to left shift a set of variables based upon an indicator variable.&lt;/P&gt;&lt;P&gt;The data are in sets, with the indicator denoted as key ( i.e. key1, b1, c1, key2, b2, c2, etc. relate to each other)&lt;/P&gt;&lt;P&gt;If the indicator variable&amp;nbsp; is missing then the set is deleted and everything should then&amp;nbsp;left shift.&lt;/P&gt;&lt;P&gt;Missing values are allowable in all variables but the indicator.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can left-shift the variables individually using the array below by running it separately for key, b &amp;amp; c. The problem is then the left shift doesn't depend on the indicator and the variables become misaligned. Is it possible to put the arrays together to make everything happen at once &lt;EM&gt;and&lt;/EM&gt; depend on the indicator variable?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm running SAS Enterprise v7.15. I would really appreciate your suggestions.&lt;/P&gt;&lt;P&gt;Best,&lt;/P&gt;&lt;P&gt;Lyn&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input id key1 key2 key3 b1 b2 b3 c1 c2 c3;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 2 3 1 2 3 1 2 3&lt;BR /&gt;2 . 2 3 1 2 3 1 2 .&lt;BR /&gt;3 1 . 3 1 2 . 1 2 3&lt;BR /&gt;4 1 2 3 . 2 3 1 . 3&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;input id key1 key2 key3 b1 b2 b3 c1 c2 c3;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 2 3 1 2 3 1 2 3&lt;BR /&gt;2 2 3 . 2 3 . 2 . .&lt;BR /&gt;3 1 3 . 1 . . 1 3 .&lt;BR /&gt;4 1 2 3 . 2 3 1 . 3&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data have2;&lt;BR /&gt;set have;&lt;BR /&gt;array j{*} key1-key3;&lt;BR /&gt;array x{3} _temporary_;&lt;BR /&gt;n=0;call missing(of x{*});&lt;BR /&gt;&lt;BR /&gt;do i=1 to dim(j);&lt;BR /&gt;if not missing(j{i}) then do;&lt;BR /&gt;n+1;x{n}=j{i};&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;&lt;BR /&gt;do i=1 to dim(j);&lt;BR /&gt;j{i}=x{i};&lt;BR /&gt;end;&lt;BR /&gt;drop i n;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Apr 2020 10:57:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-left-shift-sets-of-variables/m-p/637203#M21433</guid>
      <dc:creator>Lyn2</dc:creator>
      <dc:date>2020-04-03T10:57:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to left shift sets of variables?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-left-shift-sets-of-variables/m-p/637206#M21434</link>
      <description>&lt;P&gt;Can you explain the shifting-rules in more details, please?&lt;/P&gt;
&lt;P&gt;I don't understand why b3 is missing in the second obs.&lt;/P&gt;</description>
      <pubDate>Fri, 03 Apr 2020 11:31:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-left-shift-sets-of-variables/m-p/637206#M21434</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-04-03T11:31:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to left shift sets of variables?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-left-shift-sets-of-variables/m-p/637217#M21435</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/267248"&gt;@Lyn2&lt;/a&gt;&amp;nbsp; &amp;nbsp;Can i &lt;STRONG&gt;safely assume&lt;/STRONG&gt; the list from KEY1--C3 as numeric? If yes, the solution is rather simple&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data have;
input id key1 key2 key3 b1 b2 b3 c1 c2 c3;
cards;
1 1 2 3 1 2 3 1 2 3
2 . 2 3 1 2 3 1 2 .
3 1 . 3 1 2 . 1 2 3
4 1 2 3 . 2 3 1 . 3
;
run;

data want;
 if _n_=1 then do;
  dcl hash H (ordered: "A") ;
  h.definekey  ("_n_") ;
  h.definedata ("temp1","temp2", "temp3") ;
  h.definedone () ;
  dcl hiter hi('h');
 end;
 set have;
 array t(3,3) key1--c3;
 array temp(3);
 do _n_=1 to 3;
  do _i_=1 to 3;
  temp(_i_)=t(_i_,_n_);
  end;
  if temp1 then h.add();
 end;
 call missing(of t(*));
 do _n_=1 by 1 while(hi.next()=0);
  do _i_=1 to 3;
   t(_i_,_n_)=temp(_i_);
  end;
 end;
 h.clear();
 drop temp: _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Apr 2020 12:14:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-left-shift-sets-of-variables/m-p/637217#M21435</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-04-03T12:14:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to left shift sets of variables?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-left-shift-sets-of-variables/m-p/637229#M21436</link>
      <description>Thank you for looking at this.&lt;BR /&gt;&lt;BR /&gt;For id=2, the rule is since key1=. the original b1 and c1 get deleted.&lt;BR /&gt;Then key 2, b2, c2 and key3, b3 and c3 get left shifted.&lt;BR /&gt;Thus key3, b3 and c3 are now empty.&lt;BR /&gt;&lt;BR /&gt;Does this make sense?&lt;BR /&gt;</description>
      <pubDate>Fri, 03 Apr 2020 12:28:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-left-shift-sets-of-variables/m-p/637229#M21436</guid>
      <dc:creator>Lyn2</dc:creator>
      <dc:date>2020-04-03T12:28:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to left shift sets of variables?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-left-shift-sets-of-variables/m-p/637233#M21437</link>
      <description>&lt;P&gt;It might be simplest to just normalize (transpose) the data.&amp;nbsp; Then you are just talking about apply a WHERE clause on the KEY variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id key1 key2 key3 b1 b2 b3 c1 c2 c3;
cards;
1 1 2 3 1 2 3 1 2 3
2 . 2 3 1 2 3 1 2 .
3 1 . 3 1 2 . 1 2 3
4 1 2 3 . 2 3 1 . 3
;
proc print data=have; title 'HAVE'; run;

data tall;
  set have;
  array key[3];
  array b[3];
  array c[3];
  do i=1 to dim(key);
    _key=key[i];
    _b=b[i];
    _c=c[i];
    output;
  end;
  keep id _key _b _c;
run;

data want ;
do i=1 by 1 until(last.id);
  set tall ;
  by id;
  where _key ne . ;
  array key[3];
  array b[3];
  array c[3];
  key[i]=_key;
  b[i]=_b;
  c[i]=_c;
end;
  drop i _key _b _c;
run;

proc print data=want;  title 'WANT'; run;
title;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 441px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/37748i6F31F1A046CAFA7C/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Apr 2020 12:29:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-left-shift-sets-of-variables/m-p/637233#M21437</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-04-03T12:29:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to left shift sets of variables?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-left-shift-sets-of-variables/m-p/637236#M21438</link>
      <description>&lt;P&gt;Thank you so much for your help! It works perfectly.&lt;/P&gt;&lt;P&gt;Best,&lt;/P&gt;&lt;P&gt;Lyn&lt;/P&gt;</description>
      <pubDate>Fri, 03 Apr 2020 12:31:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-left-shift-sets-of-variables/m-p/637236#M21438</guid>
      <dc:creator>Lyn2</dc:creator>
      <dc:date>2020-04-03T12:31:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to left shift sets of variables?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-left-shift-sets-of-variables/m-p/637242#M21439</link>
      <description>&lt;P&gt;You suffer from bad data design. With a proper longitudinal structure:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id seq key b c;
cards;
1 1 1 1 1
1 2 2 2 2
1 3 3 3 3
2 1 . 1 1
2 2 2 2 2
2 3 3 3 .
3 1 1 . 1
3 2 . 1 2
3 3 3 2 3
4 1 1 . 1
4 2 2 2 .
4 3 3 3 3
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;all you need ist this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
if 0 then set have; /* keeps variable order */
set have (drop=seq where=(key ne .));
by id;
if first.id 
then seq = 1;
else seq + 1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;See Maxim 33.&lt;/P&gt;</description>
      <pubDate>Fri, 03 Apr 2020 12:43:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-left-shift-sets-of-variables/m-p/637242#M21439</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-03T12:43:49Z</dc:date>
    </item>
  </channel>
</rss>

