<?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: Shifting Values Left Across Variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Shifting-Values-Left-Across-Variables/m-p/250848#M47322</link>
    <description>&lt;P&gt;You mention that the number of JAn variables will increase over time. &amp;nbsp;Perhaps a method that uses a simple SAS variable list and what some call "infile magic". &amp;nbsp;The other methods suggested could accomplish a similar generality&amp;nbsp;using a&amp;nbsp;SAS Variable&amp;nbsp;List and the DIM function it still may be of some interest to see a little magic.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The values of the JA variables are written to the infile buffer with OPTIONS MISSING=' ' then the JA variables are read using INFILE option MISSOVER magically moving the missing value to the right.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ja;
   infile cards firstobs=2;
   input id:$3. age ja1-ja3;
   cards;
ID_ AGE Ja1 Ja2 Ja3
001 003 207 .   506
003 023 445 211 241
021 101 .   431 246
145 042 566 343 121
;;;;
   run;
proc print;
   run;
filename FT15F001 temp;
options missing=' ';
data magic;
   infile FT15F001 missover;
   input @;
   do while(not eof);
      set ja end=eof;
      _infile_ = catx(' ',of ja:);
      input @1 (ja:)(:) @;
      output;
      end;
   stop;
   parmcards;
Necessary evil
;;;;
   run;
proc print;
   run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/1927i6B5A6B8B6D38570B/image-size/original?v=mpbl-1&amp;amp;px=-1" border="0" alt="Capture.PNG" title="Capture.PNG" /&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 18 Feb 2016 15:27:11 GMT</pubDate>
    <dc:creator>data_null__</dc:creator>
    <dc:date>2016-02-18T15:27:11Z</dc:date>
    <item>
      <title>Shifting Values Left Across Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shifting-Values-Left-Across-Variables/m-p/250809#M47315</link>
      <description>&lt;P&gt;I've been struggling with a problem at work and was hoping that one of you might offer some advice.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;This is the data set I have (variable names in the top line, triple underscores indicate missing observations) :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID_ AGE Ja1 Ja2 Ja3&lt;/P&gt;&lt;P&gt;001 003 207 &lt;STRONG&gt;___ &lt;/STRONG&gt;506&lt;/P&gt;&lt;P&gt;003 023 445 211 241&lt;/P&gt;&lt;P&gt;021 101 _&lt;STRONG&gt;__&lt;/STRONG&gt;&amp;nbsp;431 246&lt;/P&gt;&lt;P&gt;145 042 566 343 121&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to use SAS to change the first data set into a data set that looks like this (ie. all of the observations have been shifted into new set of variables so that all of the missing observations are on the right-hand and all of the values are on the left):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID_ AGE Ja1 Ja2 Ja3&lt;/P&gt;&lt;P&gt;001 003 207 506&amp;nbsp;___&lt;/P&gt;&lt;P&gt;003 023 445 211 241&lt;/P&gt;&lt;P&gt;021 101 431 246&amp;nbsp;_&lt;STRONG&gt;__&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;145 042 566 343 121&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(edit: corrected the second data set.)&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One extra note: - It may not affect the solution, but there are about 200 rows in the original data set and the Ja* variables currently extend from Ja1-Ja7, but are increasing over time.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any thoughts or recommendations?&lt;/P&gt;</description>
      <pubDate>Thu, 18 Feb 2016 14:58:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shifting-Values-Left-Across-Variables/m-p/250809#M47315</guid>
      <dc:creator>TheAlbertan</dc:creator>
      <dc:date>2016-02-18T14:58:54Z</dc:date>
    </item>
    <item>
      <title>Re: Shifting Values Left Across Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shifting-Values-Left-Across-Variables/m-p/250814#M47317</link>
      <description>&lt;P&gt;Both of your examples are exactly the same? &amp;nbsp; Please also post test data in the form of a datastep so we don't have to spend time typing it all in. &amp;nbsp;Should be simply looping over an array, but provide test data and updated output first.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Feb 2016 14:08:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shifting-Values-Left-Across-Variables/m-p/250814#M47317</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-02-18T14:08:36Z</dc:date>
    </item>
    <item>
      <title>Re: Shifting Values Left Across Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shifting-Values-Left-Across-Variables/m-p/250823#M47318</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input 
	ID_ : $3.
	AGE : 8.		
	Ja1 - Ja3 : 8.;
cards;
001 003 207 . 506
003 023 445 211 241
021 101 . . 246
145 042 566 343 .
;
run;

%let a_len = 3;

data want(drop=z i l y);
set have;
array ja {&amp;amp;a_len.} ja1-ja3;
do z = 1 to &amp;amp;a_len.;	
	i=sum(i,1);
	l=sum(l,1);
	if ja{i} = . then do y = 1 to &amp;amp;a_len. while (l &amp;lt; &amp;amp;a_len.);
		l+1;
		if ja{l} = . then l+1;
		else leave;
	end;
	if i &amp;lt;= &amp;amp;a_len. and l &amp;lt;= &amp;amp;a_len. then ja{i} = ja{l};
	if l &amp;gt; &amp;amp;a_len. then ja{i} = .;
end;
output;
call missing (of _all_);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Your examples are exactly the same, but I think this is what you are trying to describe and achieve.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Feb 2016 14:40:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shifting-Values-Left-Across-Variables/m-p/250823#M47318</guid>
      <dc:creator>DanZ</dc:creator>
      <dc:date>2016-02-18T14:40:29Z</dc:date>
    </item>
    <item>
      <title>Re: Shifting Values Left Across Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shifting-Values-Left-Across-Variables/m-p/250836#M47319</link>
      <description>&lt;P&gt;Ah, in which case, you can simplfy the problem by looping backwards across the dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want (drop=i);
  set have;
  array ja{3};
  do i=3 to 2 by -1;
    if ja{i} ne . and ja{i-1}=. then do;
      ja{i-1}=ja{i};
      ja{i}=.;
    end;
  end;
run;&lt;/PRE&gt;
&lt;P&gt;Or slightly fewer keypresses:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=i tmp);
  set have;
  array ja{3};
  tmp=compress(tranwrd(catx(',',of ja{*}),".,",""));
  do i=1 to 3;
    ja{i}=input(scan(tmp,i,","),best.);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Feb 2016 15:01:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shifting-Values-Left-Across-Variables/m-p/250836#M47319</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-02-18T15:01:44Z</dc:date>
    </item>
    <item>
      <title>Re: Shifting Values Left Across Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shifting-Values-Left-Across-Variables/m-p/250848#M47322</link>
      <description>&lt;P&gt;You mention that the number of JAn variables will increase over time. &amp;nbsp;Perhaps a method that uses a simple SAS variable list and what some call "infile magic". &amp;nbsp;The other methods suggested could accomplish a similar generality&amp;nbsp;using a&amp;nbsp;SAS Variable&amp;nbsp;List and the DIM function it still may be of some interest to see a little magic.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The values of the JA variables are written to the infile buffer with OPTIONS MISSING=' ' then the JA variables are read using INFILE option MISSOVER magically moving the missing value to the right.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ja;
   infile cards firstobs=2;
   input id:$3. age ja1-ja3;
   cards;
ID_ AGE Ja1 Ja2 Ja3
001 003 207 .   506
003 023 445 211 241
021 101 .   431 246
145 042 566 343 121
;;;;
   run;
proc print;
   run;
filename FT15F001 temp;
options missing=' ';
data magic;
   infile FT15F001 missover;
   input @;
   do while(not eof);
      set ja end=eof;
      _infile_ = catx(' ',of ja:);
      input @1 (ja:)(:) @;
      output;
      end;
   stop;
   parmcards;
Necessary evil
;;;;
   run;
proc print;
   run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/1927i6B5A6B8B6D38570B/image-size/original?v=mpbl-1&amp;amp;px=-1" border="0" alt="Capture.PNG" title="Capture.PNG" /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Feb 2016 15:27:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shifting-Values-Left-Across-Variables/m-p/250848#M47322</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2016-02-18T15:27:11Z</dc:date>
    </item>
    <item>
      <title>Re: Shifting Values Left Across Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shifting-Values-Left-Across-Variables/m-p/251006#M47362</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ja;
   infile cards firstobs=2;
   input id:$3. age ja1-ja3;
   cards;
ID_ AGE Ja1 Ja2 Ja3
001 003 207 .   506
003 023 445 211 241
021 101 .   431 246
145 042 566 343 121
;;;;
   run;
data want;
 set ja;
 array j{*} ja1-ja3;
 array x{3} _temporary_;
 n=0;call missing(of x{*});
 
 do i=1 to dim(j);
  if not missing(j{i}) then do;
   n+1;x{n}=j{i};
  end;
 end;
 
 do i=1 to dim(j);
  j{i}=x{i};
 end;
 
 drop i n;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 19 Feb 2016 02:33:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shifting-Values-Left-Across-Variables/m-p/251006#M47362</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-02-19T02:33:31Z</dc:date>
    </item>
    <item>
      <title>Re: Shifting Values Left Across Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shifting-Values-Left-Across-Variables/m-p/251007#M47363</link>
      <description>&lt;P&gt;John King(data _null_),&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good Code, which remind me the code written by TOM before .&lt;/P&gt;
&lt;P&gt;1)What if there are lots of variables ,and your&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token function"&gt;catx&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;' '&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;of ja:&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;can't hold them all together ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2)And what if the mssing value is like .A - .Z ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards&lt;/P&gt;</description>
      <pubDate>Fri, 19 Feb 2016 02:45:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shifting-Values-Left-Across-Variables/m-p/251007#M47363</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-02-19T02:45:00Z</dc:date>
    </item>
    <item>
      <title>Re: Shifting Values Left Across Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shifting-Values-Left-Across-Variables/m-p/251100#M47381</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp﻿&lt;/a&gt;&amp;nbsp;you know the answer to both of those questions. There would have to be an very large number of variables break it and I doubt that anyone else in this tread even knows special missing values are.&lt;/P&gt;</description>
      <pubDate>Fri, 19 Feb 2016 12:56:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shifting-Values-Left-Across-Variables/m-p/251100#M47381</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2016-02-19T12:56:27Z</dc:date>
    </item>
  </channel>
</rss>

