<?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: Is there any way to transpose the observation into variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Is-there-any-way-to-transpose-the-observation-into-variable/m-p/335694#M76026</link>
    <description>&lt;P&gt;Hi, or this? Provided data input already ordered by date.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data real; 
        set real;
	by department notsorted;
	dif_sale = dif(sale);
	if first.department then do; dif_sale = .; end;
	input date $ 1-9 department $ sale ;
cards;
30nov2016 west 175
31dec2016 west 134
31jan2017 west 124
30nov2016 east 198
31dec2016 east 182
31jan2017 east 117
30nov2016 south 200
31dec2016 south 203
31jan2017 south 205
30nov2016 north 199
31dec2016 north 194
31jan2017 north 106
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 24 Feb 2017 18:27:03 GMT</pubDate>
    <dc:creator>Miracle</dc:creator>
    <dc:date>2017-02-24T18:27:03Z</dc:date>
    <item>
      <title>Is there any way to transpose the observation into variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-any-way-to-transpose-the-observation-into-variable/m-p/335664#M76017</link>
      <description>&lt;P&gt;What I am trying to do is to calculate the differences&amp;nbsp;of Sales #&amp;nbsp;for each&amp;nbsp;department between&amp;nbsp;3 different dates. The codes seem work in the dataset "test".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, the real dataset I have consists of&amp;nbsp;3 variables and&amp;nbsp;12 observations shown as below in the dataset "real".&amp;nbsp;My question is&amp;nbsp;is there any&amp;nbsp;way to&amp;nbsp;transpose the observation of date into variable to make the dataset looks the same as&amp;nbsp;"test", so I can calculate the differences in Sales for each period.&amp;nbsp;Or perhaps there's a way to achieve the same result directly from dataset "real"?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any suggestions are gladly appreciated.&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;DATA&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; TEST;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;INPUT&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; DEPARTMENT $ SALE1 SALE2 SALE3 ;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;CARDS&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;WEST 175 134 124&lt;/P&gt;&lt;P&gt;EAST 198 182 117&lt;/P&gt;&lt;P&gt;SOUTH 200 203 205&lt;/P&gt;&lt;P&gt;NORTH 199 194 106&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;RUN&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;DATA&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; XYZ;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;SET&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; TEST;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;ARRAY&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; SALE(*) SALE1 - SALE3;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;ARRAY&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; SALE_DIFF(&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;2&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;DO&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; I = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;TO&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; DIM(SALE_DIFF);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;SALE_DIFF(I) = SALE(I + &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;) - SALE(I);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;END&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;DROP&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; I;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;RUN&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;PROC&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;PRINT&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;DATA&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=XYZ &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;NOOBS&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;VAR&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; DEPARTMENT SALE_DIFF1 - SALE_DIFF2;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;RUN&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;DATA&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; REAL;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;INPUT&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; DATE $ &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;-&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;9&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt; DEPARTMENT $ SALE ;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;CARDS&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;30NOV2016 WEST 175&lt;/P&gt;&lt;P&gt;31DEC2016 WEST 134&lt;/P&gt;&lt;P&gt;31JAN2017 WEST 124&lt;/P&gt;&lt;P&gt;30NOV2016 EAST 198&lt;/P&gt;&lt;P&gt;31DEC2016 EAST 182&lt;/P&gt;&lt;P&gt;31JAN2017 EAST 117&lt;/P&gt;&lt;P&gt;30NOV2016 SOUTH 200&lt;/P&gt;&lt;P&gt;31DEC2016 SOUTH 203&lt;/P&gt;&lt;P&gt;31JAN2017 SOUTH 205&lt;/P&gt;&lt;P&gt;30NOV2016 NORTH 199&lt;/P&gt;&lt;P&gt;31DEC2016 NORTH 194&lt;/P&gt;&lt;P&gt;31JAN2017 NORTH 106&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;RUN&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2017 17:07:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-any-way-to-transpose-the-observation-into-variable/m-p/335664#M76017</guid>
      <dc:creator>LL5</dc:creator>
      <dc:date>2017-02-24T17:07:57Z</dc:date>
    </item>
    <item>
      <title>Re: Is there any way to transpose the observation into variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-any-way-to-transpose-the-observation-into-variable/m-p/335671#M76019</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=real prefix=D out=realtrans (drop=_name_);
by department;
id date;
var sale;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2017 17:38:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-any-way-to-transpose-the-observation-into-variable/m-p/335671#M76019</guid>
      <dc:creator>nehalsanghvi</dc:creator>
      <dc:date>2017-02-24T17:38:59Z</dc:date>
    </item>
    <item>
      <title>Re: Is there any way to transpose the observation into variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-any-way-to-transpose-the-observation-into-variable/m-p/335677#M76021</link>
      <description>&lt;P&gt;If your data are already in the order shown, then the following code will work. If they're not in that order, then you would have to sort the file first:&lt;/P&gt;
&lt;PRE&gt;proc transpose data=real prefix=sale out=realtrans (drop=_name_);
  by department notsorted;
  var sale;
run;&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2017 17:55:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-any-way-to-transpose-the-observation-into-variable/m-p/335677#M76021</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-02-24T17:55:34Z</dc:date>
    </item>
    <item>
      <title>Re: Is there any way to transpose the observation into variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-any-way-to-transpose-the-observation-into-variable/m-p/335680#M76023</link>
      <description>&lt;P&gt;Keep the original order of the REAL dataset and use the lag() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set real;
by department notsorted;
diff = sale - lag(sale);
if first.department then diff = .;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Feb 2017 18:01:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-any-way-to-transpose-the-observation-into-variable/m-p/335680#M76023</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-02-24T18:01:46Z</dc:date>
    </item>
    <item>
      <title>Re: Is there any way to transpose the observation into variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-any-way-to-transpose-the-observation-into-variable/m-p/335682#M76024</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Difference is sales for store1-store2 and store3-store2

Nicely layed out question

Other solutions could use lag or 'by not sorted'
No need to transpose.
My solution will stop processing if you have more then three 'stores' .

inspired by
https://goo.gl/OgQQNz
https://communities.sas.com/t5/Base-SAS-Programming/Is-there-any-way-to-transpose-the-observation-into-variable/m-p/335664

HAVE
====

Up to 40 obs WORK.HAVE total obs=12

Obs      DATE       DEPARTMENT    SALE

  1    30NOV2016      WEST         175
  2    31DEC2016      WEST         134
  3    31JAN2017      WEST         124

  4    30NOV2016      EAST         198
  5    31DEC2016      EAST         182
  6    31JAN2017      EAST         117

  7    30NOV2016      SOUTH        200
  8    31DEC2016      SOUTH        203
  9    31JAN2017      SOUTH        205

 10    30NOV2016      NORTH        199
 11    31DEC2016      NORTH        194
 12    31JAN2017      NORTH        106


WANT
====

Up to 40 obs from want total obs=4

              SALE_    SALE_
DEPARTMENT    DIFF1    DIFF2

  WEST         -41      -10
  EAST         -16      -65
  SOUTH          3        2
  NORTH         -5      -88

DETAILS
=======
                                                ????
Obs    DEPARTMENT    DIF1  Sale1-Sale2  DIF2  Sale3-Sale2

 1       WEST          41  175-134      -10    124-134
 2       EAST          16  198-182      -65    117-182
 3       SOUTH         -3  200-203        2    205-203
 4       NORTH          5  199-194      -88    106-194

WORKING CODE
===========

     select;
       when  (mod(_n_,3)=2) vals[1]=sale;
       when  (mod(_n_,3)=1) vals[2]=sale;
       when  (mod(_n_,3)=0) do;
           dif1=vals[2]-vals[1];
           dif2=sale-vals[1];
           output;
       end;
     end; * leave off otherwise to force error;


FULL SOLUTION
=============

DATA have;
INPUT DATE $ 1-9 DEPARTMENT $ SALE ;
CARDS;
30NOV2016 WEST 175
31DEC2016 WEST 134
31JAN2017 WEST 124
30NOV2016 EAST 198
31DEC2016 EAST 182
31JAN2017 EAST 117
30NOV2016 SOUTH 200
31DEC2016 SOUTH 203
31JAN2017 SOUTH 205
30NOV2016 NORTH 199
31DEC2016 NORTH 194
31JAN2017 NORTH 106
;
RUN;

data want;
  array vals[2] _temporary_;
  set have;
  select;
    when  (mod(_n_,3)=2) vals[1]=sale;
    when  (mod(_n_,3)=1) vals[2]=sale;
    when  (mod(_n_,3)=0) do;
        dif1=vals[2]-vals[1];
        dif2=sale-vals[1];
        output;
    end;
  end; * leave off otherwise to force error;
  keep department dif:;
run;quit;

Up to 40 obs from want total obs=4

Obs    DEPARTMENT    DIF1    DIF2

 1       WEST          41     -10
 2       EAST          16     -65
 3       SOUTH         -3       2
 4       NORTH          5     -88


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Feb 2017 18:06:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-any-way-to-transpose-the-observation-into-variable/m-p/335682#M76024</guid>
      <dc:creator>rogerjdeangelis</dc:creator>
      <dc:date>2017-02-24T18:06:22Z</dc:date>
    </item>
    <item>
      <title>Re: Is there any way to transpose the observation into variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-any-way-to-transpose-the-observation-into-variable/m-p/335694#M76026</link>
      <description>&lt;P&gt;Hi, or this? Provided data input already ordered by date.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data real; 
        set real;
	by department notsorted;
	dif_sale = dif(sale);
	if first.department then do; dif_sale = .; end;
	input date $ 1-9 department $ sale ;
cards;
30nov2016 west 175
31dec2016 west 134
31jan2017 west 124
30nov2016 east 198
31dec2016 east 182
31jan2017 east 117
30nov2016 south 200
31dec2016 south 203
31jan2017 south 205
30nov2016 north 199
31dec2016 north 194
31jan2017 north 106
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Feb 2017 18:27:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-any-way-to-transpose-the-observation-into-variable/m-p/335694#M76026</guid>
      <dc:creator>Miracle</dc:creator>
      <dc:date>2017-02-24T18:27:03Z</dc:date>
    </item>
    <item>
      <title>Re: Is there any way to transpose the observation into variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-any-way-to-transpose-the-observation-into-variable/m-p/335695#M76027</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Difference is sales for store1-store2 and store3-store2

Nicely layed out question

Other solutions could use lag or 'by not sorted'
No need to transpose.
My solution will stop processing if you have more then three 'stores' .

inspired by
https://goo.gl/OgQQNz
https://communities.sas.com/t5/Base-SAS-Programming/Is-there-any-way-to-transpose-the-observation-into-variable/m-p/335664

HAVE
====

Up to 40 obs WORK.HAVE total obs=12

Obs      DATE       DEPARTMENT    SALE

  1    30NOV2016      WEST         175
  2    31DEC2016      WEST         134
  3    31JAN2017      WEST         124

  4    30NOV2016      EAST         198
  5    31DEC2016      EAST         182
  6    31JAN2017      EAST         117

  7    30NOV2016      SOUTH        200
  8    31DEC2016      SOUTH        203
  9    31JAN2017      SOUTH        205

 10    30NOV2016      NORTH        199
 11    31DEC2016      NORTH        194
 12    31JAN2017      NORTH        106


WANT
====

Up to 40 obs from want total obs=4

              SALE_    SALE_
DEPARTMENT    DIFF1    DIFF2

  WEST         -41      -10
  EAST         -16      -65
  SOUTH          3        2
  NORTH         -5      -88

DETAILS
=======
                                                ????
Obs    DEPARTMENT    DIF1  Sale1-Sale2  DIF2  Sale3-Sale2

 1       WEST          41  175-134      -10    124-134
 2       EAST          16  198-182      -65    117-182
 3       SOUTH         -3  200-203        2    205-203
 4       NORTH          5  199-194      -88    106-194

WORKING CODE
===========

     select;
       when  (mod(_n_,3)=2) vals[1]=sale;
       when  (mod(_n_,3)=1) vals[2]=sale;
       when  (mod(_n_,3)=0) do;
           dif1=vals[2]-vals[1];
           dif2=sale-vals[1];
           output;
       end;
     end; * leave off otherwise to force error;


FULL SOLUTION
=============

DATA have;
INPUT DATE $ 1-9 DEPARTMENT $ SALE ;
CARDS;
30NOV2016 WEST 175
31DEC2016 WEST 134
31JAN2017 WEST 124
30NOV2016 EAST 198
31DEC2016 EAST 182
31JAN2017 EAST 117
30NOV2016 SOUTH 200
31DEC2016 SOUTH 203
31JAN2017 SOUTH 205
30NOV2016 NORTH 199
31DEC2016 NORTH 194
31JAN2017 NORTH 106
;
RUN;

data want;
  array vals[2] _temporary_;
  set have;
  select;
    when  (mod(_n_,3)=2) vals[1]=sale;
    when  (mod(_n_,3)=1) vals[2]=sale;
    when  (mod(_n_,3)=0) do;
        dif1=vals[2]-vals[1];
        dif2=sale-vals[1];
        output;
    end;
  end; * leave off otherwise to force error;
  keep department dif:;
run;quit;

Up to 40 obs from want total obs=4

Obs    DEPARTMENT    DIF1    DIF2

 1       WEST          41     -10
 2       EAST          16     -65
 3       SOUTH         -3       2
 4       NORTH          5     -88


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2017 18:28:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-any-way-to-transpose-the-observation-into-variable/m-p/335695#M76027</guid>
      <dc:creator>rogerjdeangelis</dc:creator>
      <dc:date>2017-02-24T18:28:12Z</dc:date>
    </item>
    <item>
      <title>Re: Is there any way to transpose the observation into variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-any-way-to-transpose-the-observation-into-variable/m-p/335718#M76031</link>
      <description>&lt;P&gt;&lt;SPAN style="background: white; color: black; font-family: &amp;quot;Arial&amp;quot;,&amp;quot;sans-serif&amp;quot;;"&gt;&lt;FONT size="3"&gt;Hi Art297, I tried your approach and it worked perfectly, Thanks! &lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background: white; color: black; font-family: &amp;quot;Arial&amp;quot;,&amp;quot;sans-serif&amp;quot;;"&gt;&lt;FONT size="3"&gt;Since I am not that familiar with Proc Transpose, it seems the variable names look a little bit weird shown as below, and I need to change the array name to achieve the end result. Is there any better way to assign the array name in the dataset Final? &lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background: white; color: black; font-family: &amp;quot;Arial&amp;quot;,&amp;quot;sans-serif&amp;quot;;"&gt;&lt;FONT size="3"&gt;Again, appreciated for your advices. &lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;DATA&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; REAL;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;INPUT&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; DATE $ &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;-&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;10&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt; DEPARTMENT $ SALE ;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;CARDS&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;30NOV2016 WEST 175&lt;/P&gt;&lt;P&gt;31DEC2016 WEST 134&lt;/P&gt;&lt;P&gt;31JAN2017 WEST 124&lt;/P&gt;&lt;P&gt;30NOV2016 EAST 198&lt;/P&gt;&lt;P&gt;31DEC2016 EAST 182&lt;/P&gt;&lt;P&gt;31JAN2017 EAST 117&lt;/P&gt;&lt;P&gt;30NOV2016 SOUTH 200&lt;/P&gt;&lt;P&gt;31DEC2016 SOUTH 203&lt;/P&gt;&lt;P&gt;31JAN2017 SOUTH 205&lt;/P&gt;&lt;P&gt;30NOV2016 NORTH 199&lt;/P&gt;&lt;P&gt;31DEC2016 NORTH 194&lt;/P&gt;&lt;P&gt;31JAN2017 NORTH 106&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;RUN&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;sort&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;data&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=real &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;out&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=real1;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;by&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; department;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;transpose&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;data&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=real1 &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;prefix&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=D &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;out&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=realtrans (&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;drop&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=_name_);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;by&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; department;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;id&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; date;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;var&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; sale;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;TITLE&lt;/FONT&gt; &lt;FONT color="#800080" face="Courier New" size="3"&gt;' AFTER TRANSPOSE'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;PROC&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;PRINT&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;DATA&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=realtrans &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;NOOBS&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;RUN&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;DATA&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; FINAL;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;SET&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; realtrans;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;ARRAY&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; SALE(&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;3&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;) D30NOV2016 D31DEC2016 D31JAN2017; &lt;/FONT&gt;&lt;FONT color="#008000" face="Courier New" size="3"&gt;/* Is there any better or efficient&amp;nbsp;way to assign&amp;nbsp;the array name here ? I tried D30NOV2016 - D31JAN2017 but it didn't work*/&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;ARRAY&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; SALE_DIFF(&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;2&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;DO&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; I = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;TO&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; DIM(SALE_DIFF);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;SALE_DIFF(I) = SALE(I + &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;) - SALE(I);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;END&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;DROP&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; I;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;RUN&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;TITLE&lt;/FONT&gt; &lt;FONT color="#800080" face="Courier New" size="3"&gt;'FINAL REPORT'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;PROC&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;PRINT&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;DATA&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=XYZ &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;NOOBS&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;VAR&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; DEPARTMENT SALE_DIFF1 - SALE_DIFF2;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;RUN&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;&amp;nbsp;AFTER&amp;nbsp;TRANSPOSE&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;DEPARTMENT&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;D30NOV2016&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;D31DEC2016&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;D31JAN2017&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;EAST&lt;/TD&gt;&lt;TD&gt;198&lt;/TD&gt;&lt;TD&gt;182&lt;/TD&gt;&lt;TD&gt;117&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;NORTH&lt;/TD&gt;&lt;TD&gt;199&lt;/TD&gt;&lt;TD&gt;194&lt;/TD&gt;&lt;TD&gt;106&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;SOUTH&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;TD&gt;203&lt;/TD&gt;&lt;TD&gt;205&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;WEST&lt;/TD&gt;&lt;TD&gt;175&lt;/TD&gt;&lt;TD&gt;134&lt;/TD&gt;&lt;TD&gt;124&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;FINAL&amp;nbsp;REPORT&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;DEPARTMENT&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;SALE_DIFF1&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;SALE_DIFF2&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;WEST&lt;/TD&gt;&lt;TD&gt;-41&lt;/TD&gt;&lt;TD&gt;-10&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;EAST&lt;/TD&gt;&lt;TD&gt;-16&lt;/TD&gt;&lt;TD&gt;-65&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;SOUTH&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;NORTH&lt;/TD&gt;&lt;TD&gt;-5&lt;/TD&gt;&lt;TD&gt;-88&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Fri, 24 Feb 2017 19:40:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-any-way-to-transpose-the-observation-into-variable/m-p/335718#M76031</guid>
      <dc:creator>LL5</dc:creator>
      <dc:date>2017-02-24T19:40:00Z</dc:date>
    </item>
    <item>
      <title>Re: Is there any way to transpose the observation into variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-any-way-to-transpose-the-observation-into-variable/m-p/335722#M76032</link>
      <description>&lt;P&gt;You are getting the odd stuff in your names because of how you are calling proc transpose.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want the final names to be sales_diff1 to sales_diff2, just use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sort data=real;&lt;BR /&gt; by department date;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc transpose data=real prefix=sale_ out=realtrans (drop=_name_);&lt;BR /&gt; by department notsorted;&lt;BR /&gt; var sale;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data realtrans (drop=sale_1-sale_3);&lt;BR /&gt; set realtrans;&lt;BR /&gt; sales_diff1=sale_1-sale_2;&lt;BR /&gt; sales_diff2=sale_2-sale_3;&lt;BR /&gt;run;&lt;BR /&gt;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2017 19:54:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-any-way-to-transpose-the-observation-into-variable/m-p/335722#M76032</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-02-24T19:54:34Z</dc:date>
    </item>
    <item>
      <title>Re: Is there any way to transpose the observation into variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-any-way-to-transpose-the-observation-into-variable/m-p/335723#M76033</link>
      <description>&lt;P&gt;The ID statement automatically creates the variable names based on distinct values present in the Date column. Since the values begin with a number and SAS does not allow variable names to begin with a number, we add on a prefix (in this case, D is the prefix I used). So the first variable is D+30NOV2016&amp;nbsp;= D30NOV2016, and so on. Without providing a prefix, SAS would just put an underscore at the beginning of the variable names like _30NOV2016.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;With regard to automatically pulling in var names in your array, this is how you can do it:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=real prefix=D_ out=realtrans (drop=_name_); /* added an underscore so that query below works, want to distinguish these variables from Department */
by department notsorted;
id date;
var sale;
run;

proc sql;
select name into :namevarlist separated by ' '
from dictionary.columns
where memname='REALTRANS'
and substr(name,1,2) ='D_';
quit;

DATA FINAL;
SET realtrans;
ARRAY SALE(3) &amp;amp;namevarlist.; 
ARRAY SALE_DIFF(2);
DO I = 1 TO DIM(SALE_DIFF);
	SALE_DIFF(I) = SALE(I + 1) - SALE(I);
END;
DROP I;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Feb 2017 19:56:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-any-way-to-transpose-the-observation-into-variable/m-p/335723#M76033</guid>
      <dc:creator>nehalsanghvi</dc:creator>
      <dc:date>2017-02-24T19:56:16Z</dc:date>
    </item>
    <item>
      <title>Re: Is there any way to transpose the observation into variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-any-way-to-transpose-the-observation-into-variable/m-p/336318#M76229</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/89004"&gt;@LL5&lt;/a&gt;, please mark one of the responses as the solution.</description>
      <pubDate>Mon, 27 Feb 2017 19:43:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-any-way-to-transpose-the-observation-into-variable/m-p/336318#M76229</guid>
      <dc:creator>nehalsanghvi</dc:creator>
      <dc:date>2017-02-27T19:43:30Z</dc:date>
    </item>
  </channel>
</rss>

