<?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: Reshaping a large dataset from long to wide with many variables in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Reshaping-a-large-dataset-from-long-to-wide-with-many-variables/m-p/678615#M79418</link>
    <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; The formats are the least of your worries. When you do a transpose with VAR _ALL_; if you have a mix of numeric and character variables, all your numeric variables will be converted to character.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; As an example, consider this:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_1-1598055314541.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/48549i8D5BCABDC00544DD/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_1-1598055314541.png" alt="Cynthia_sas_1-1598055314541.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think the result after the second transpose is what you want, but the challenge is that I only had 2 properties and 3 years for each property and then 4 variables (VAR1-VAR4) for each row. With 150 variables on each row, this would quickly become unmanageable and all your numeric variables would now be character too. The program actually does 2 transposes. The first transpose gets you a variable for year and then _NAME_ holds the value of the original variable name. Next, with a DATA step, you make a variable called NEWNAME that can be used in a second transpose,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But still I think your original structure is more useful for most SAS procedures.&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;
&lt;P&gt;here's the program I used to make the fake data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; data fakedata;
  infile datalines dlm=',';
  input propertyid $ year var1 var2 $ var3 var4 $;
return;
datalines;
Prop1,2010,101,ABC,102,DEF
Prop1,2011,201,GHI,202,JKL
Prop1,2012,301,MNO,302,PQR
Prop2,2010,401,KJI,402,NML
Prop2,2011,501,OPQ,502,TSR
Prop2,2012,601,WUV,602,ZYX
;
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>Sat, 22 Aug 2020 00:18:56 GMT</pubDate>
    <dc:creator>Cynthia_sas</dc:creator>
    <dc:date>2020-08-22T00:18:56Z</dc:date>
    <item>
      <title>Reshaping a large dataset from long to wide with many variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Reshaping-a-large-dataset-from-long-to-wide-with-many-variables/m-p/678208#M79401</link>
      <description>&lt;P&gt;I have a large dataset in SAS that I need to change to wide from long.&lt;/P&gt;&lt;P&gt;Basically, the data is sorted with multiple observations for each property for about 10 different years with around 150 variables. I would like to make it so each variable is a different year and have one observation per property. Each variable is uniquely named and I want to keep the name in it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I have:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Property&amp;nbsp;&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;Year&amp;nbsp;&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;Var1&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;...&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;Var150&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Prop1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2010&amp;nbsp; 100 ...&amp;nbsp; ABC&lt;/P&gt;&lt;P&gt;Prop1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2011&amp;nbsp; 101 ...&amp;nbsp; DEF&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;Prop1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2017&amp;nbsp; 138 ... XYZ&lt;/P&gt;&lt;P&gt;Prop2 ...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Property&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;Var1_2010&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;...&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;Var1_2017&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;...&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;Var150_2010&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;...&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;Var150_2017&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Prop1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;100&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ...&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 138&amp;nbsp; &amp;nbsp; &amp;nbsp;...&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ABC&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;...&amp;nbsp; &amp;nbsp; &amp;nbsp; XYZ&lt;/P&gt;&lt;P&gt;Prop2 ...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried using Proc Transpose, but I cannot find a way to concatenate the year at the end of each original variable name.&lt;/P&gt;&lt;P&gt;I also tried:&lt;/P&gt;&lt;PRE class="prettyprint-override lang-html snippet-code-html"&gt;&lt;CODE&gt;proc sort data=hotels;
by propertyID year;
run;

proc transpose data=hotels out=hotels_wide;
by propertyID year;
var _all_;
run;

proc transpose data=hotels_wide delimiter=_ out=hotels_wide_full(drop=_name_);
by propertyID;
var col1;
id _name_ year;
run;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;But the last block gave me an error telling me there are multiple occurrences of each variable for each propertyID:&amp;nbsp;ERROR: The ID value "RmsServiceChargeDistribution_2E3" occurs twice in the same BY group.&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;A shortcoming of this would also be losing the formats of each variable.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Aug 2020 18:30:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Reshaping-a-large-dataset-from-long-to-wide-with-many-variables/m-p/678208#M79401</guid>
      <dc:creator>jss539</dc:creator>
      <dc:date>2020-08-20T18:30:05Z</dc:date>
    </item>
    <item>
      <title>Re: Reshaping a large dataset from long to wide with many variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Reshaping-a-large-dataset-from-long-to-wide-with-many-variables/m-p/678605#M79416</link>
      <description>Hi:&lt;BR /&gt;  I don't see how this is going from WIDE to LONG. Right now, you have multiple rows for PROP1 and multiple rows for PROP2, one for each year. I would consider that already LONG. If you change it so that you have 1 value for Property and up to 150 separate variables for each year, that seems to me you are going from sort of wide, but mostly long to really, really wide and not long at all.&lt;BR /&gt;&lt;BR /&gt;Or, to do the math based on your explanation, if you had a dataset with only 3 rows for Prop1 and 150 variables for each row then that's this: &lt;BR /&gt;PROP1 2010 with 150 variables&lt;BR /&gt;PROP1 2011 with 150 variables&lt;BR /&gt;PROP1 2012 with 150 variables &lt;BR /&gt;&lt;BR /&gt;  But, if you now collapse those 3 rows into just 1 row for PROP1, then you'll now have 1 row with 450 variables. That seems like a much wider dataset to me. What do you need to do with this data. Most SAS procedures would be much happier dealing with the data as you have it now, than in the structure you're proposing. What is the downstream process for this very, very wide table?&lt;BR /&gt;&lt;BR /&gt;Cynthia</description>
      <pubDate>Fri, 21 Aug 2020 22:49:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Reshaping-a-large-dataset-from-long-to-wide-with-many-variables/m-p/678605#M79416</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2020-08-21T22:49:06Z</dc:date>
    </item>
    <item>
      <title>Re: Reshaping a large dataset from long to wide with many variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Reshaping-a-large-dataset-from-long-to-wide-with-many-variables/m-p/678614#M79417</link>
      <description>&lt;P&gt;Converting this long data set to wide will give you a programming nightmare. There's no easy way to handle such a wide data set, other than hard coding all the variable names when you want to use them. You'd be much better off leaving the data as long, and then built in SAS methods, such as BY variables and PROC REPORT and many others, make handling such a long data set very easy. Why do you need this data set as a wide data set?&lt;/P&gt;</description>
      <pubDate>Sat, 22 Aug 2020 00:17:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Reshaping-a-large-dataset-from-long-to-wide-with-many-variables/m-p/678614#M79417</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-08-22T00:17:19Z</dc:date>
    </item>
    <item>
      <title>Re: Reshaping a large dataset from long to wide with many variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Reshaping-a-large-dataset-from-long-to-wide-with-many-variables/m-p/678615#M79418</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; The formats are the least of your worries. When you do a transpose with VAR _ALL_; if you have a mix of numeric and character variables, all your numeric variables will be converted to character.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; As an example, consider this:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_1-1598055314541.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/48549i8D5BCABDC00544DD/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_1-1598055314541.png" alt="Cynthia_sas_1-1598055314541.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think the result after the second transpose is what you want, but the challenge is that I only had 2 properties and 3 years for each property and then 4 variables (VAR1-VAR4) for each row. With 150 variables on each row, this would quickly become unmanageable and all your numeric variables would now be character too. The program actually does 2 transposes. The first transpose gets you a variable for year and then _NAME_ holds the value of the original variable name. Next, with a DATA step, you make a variable called NEWNAME that can be used in a second transpose,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But still I think your original structure is more useful for most SAS procedures.&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;
&lt;P&gt;here's the program I used to make the fake data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; data fakedata;
  infile datalines dlm=',';
  input propertyid $ year var1 var2 $ var3 var4 $;
return;
datalines;
Prop1,2010,101,ABC,102,DEF
Prop1,2011,201,GHI,202,JKL
Prop1,2012,301,MNO,302,PQR
Prop2,2010,401,KJI,402,NML
Prop2,2011,501,OPQ,502,TSR
Prop2,2012,601,WUV,602,ZYX
;
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>Sat, 22 Aug 2020 00:18:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Reshaping-a-large-dataset-from-long-to-wide-with-many-variables/m-p/678615#M79418</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2020-08-22T00:18:56Z</dc:date>
    </item>
    <item>
      <title>Re: Reshaping a large dataset from long to wide with many variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Reshaping-a-large-dataset-from-long-to-wide-with-many-variables/m-p/678622#M79419</link>
      <description>&lt;P&gt;The code is fine.&amp;nbsp; But the example does not reflect the actual data.&amp;nbsp; The error messages makes if very clear what the issue is.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;The original dataset has a variable with a name that is already waaaaaaay too long.&lt;/STRONG&gt;&lt;/P&gt;
&lt;PRE&gt;1234567890123456789012345678012
RmsServiceChargeDistribution
&lt;/PRE&gt;
&lt;P&gt;Since there is not room to add 5 more characters to the end of the existing name the year was reduced to 3 characters.&amp;nbsp; But that means that the new name is no longer unique.&lt;/P&gt;
&lt;P&gt;Either use more reasonable lengths for the variable names to start with, or you will need to add a step to create unique names for the new variables.&lt;/P&gt;</description>
      <pubDate>Sat, 22 Aug 2020 15:00:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Reshaping-a-large-dataset-from-long-to-wide-with-many-variables/m-p/678622#M79419</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-08-22T15:00:00Z</dc:date>
    </item>
  </channel>
</rss>

