<?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 How to transpose and rename variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-transpose-and-rename-variables/m-p/760282#M240398</link>
    <description>&lt;P&gt;I have a dataset (excerpt shown below):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA have;
input id	year	Latitude	Longitude
;
DATALINES;
1049	1995	34.289001	-85.970065
1049	1999	34.289001	-85.970065
1073	1990	33.386389	-86.816667
1073	1995	33.331111	-87.003611
1073	1995	33.386389	-86.816667
1073	1995	33.578333	-86.773889
1073	1996	33.331111	-87.003611
1073	1996	33.386389	-86.816667
1073	1996	33.704722	-86.669167
1073	1996	33.578333	-86.773889
1073	1998	33.485556	-86.915
1073	1998	33.386389	-86.816667
1073	1999	33.331111	-87.003611
1073	1999	33.386389	-86.816667


;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want to transpose the dataset to be as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA want;
input id	year	Latitude1	Longitude1	Latitude2	Longitude2	Latitude3	Longitude3	Latitude4	Longitude4

;
DATALINES;
1049	1995	34.289001	-85.970065	.	.	.	.	.	.
1049	1999	34.289001	-85.970065	.	.	.	.	.	.
1073	1990	33.386389	-86.816667	.	.	.	.	.	.
1073	1995	33.331111	-87.003611	33.386389	-86.816667	33.578333	-86.773889	.	.
1073	1996	33.331111	-87.003611	33.386389	-86.816667	33.704722	-86.669167	33.578333	-86.773889
1073	1998	33.485556	-86.915	33.386389	-86.816667	.	.	.	.
1073	1999	33.331111	-87.003611	33.386389	-86.816667	.	.	.	.

;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;What I've tried is using the following code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have out=want;

by id year;
var latitude longitude;

run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;However, the result is not quite what I wanted. In particular, the full dataset could have many latitude/longitude pairs (much more than 4 shown in the example), so I would like the naming of the columns to be from latitude1, longitude1, latitude2, longitude2, ..., up until the last pair.&lt;/P&gt;</description>
    <pubDate>Mon, 09 Aug 2021 01:04:52 GMT</pubDate>
    <dc:creator>elbarto</dc:creator>
    <dc:date>2021-08-09T01:04:52Z</dc:date>
    <item>
      <title>How to transpose and rename variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-transpose-and-rename-variables/m-p/760282#M240398</link>
      <description>&lt;P&gt;I have a dataset (excerpt shown below):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA have;
input id	year	Latitude	Longitude
;
DATALINES;
1049	1995	34.289001	-85.970065
1049	1999	34.289001	-85.970065
1073	1990	33.386389	-86.816667
1073	1995	33.331111	-87.003611
1073	1995	33.386389	-86.816667
1073	1995	33.578333	-86.773889
1073	1996	33.331111	-87.003611
1073	1996	33.386389	-86.816667
1073	1996	33.704722	-86.669167
1073	1996	33.578333	-86.773889
1073	1998	33.485556	-86.915
1073	1998	33.386389	-86.816667
1073	1999	33.331111	-87.003611
1073	1999	33.386389	-86.816667


;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want to transpose the dataset to be as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA want;
input id	year	Latitude1	Longitude1	Latitude2	Longitude2	Latitude3	Longitude3	Latitude4	Longitude4

;
DATALINES;
1049	1995	34.289001	-85.970065	.	.	.	.	.	.
1049	1999	34.289001	-85.970065	.	.	.	.	.	.
1073	1990	33.386389	-86.816667	.	.	.	.	.	.
1073	1995	33.331111	-87.003611	33.386389	-86.816667	33.578333	-86.773889	.	.
1073	1996	33.331111	-87.003611	33.386389	-86.816667	33.704722	-86.669167	33.578333	-86.773889
1073	1998	33.485556	-86.915	33.386389	-86.816667	.	.	.	.
1073	1999	33.331111	-87.003611	33.386389	-86.816667	.	.	.	.

;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;What I've tried is using the following code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have out=want;

by id year;
var latitude longitude;

run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;However, the result is not quite what I wanted. In particular, the full dataset could have many latitude/longitude pairs (much more than 4 shown in the example), so I would like the naming of the columns to be from latitude1, longitude1, latitude2, longitude2, ..., up until the last pair.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Aug 2021 01:04:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-transpose-and-rename-variables/m-p/760282#M240398</guid>
      <dc:creator>elbarto</dc:creator>
      <dc:date>2021-08-09T01:04:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to transpose and rename variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-transpose-and-rename-variables/m-p/760286#M240401</link>
      <description>&lt;P&gt;What are you going to to do with that wide data set? How will you program anything to us it if you do not know how many pairs will be involved for any given id?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you describe what you intend to do that may help. Most SAS procedures will be much happier with the long format you currently have.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is as close as I'm willing to spend any time working on at the moment without knowing how this is to be used.&lt;/P&gt;
&lt;PRE&gt;DATA have;
input id	year	Latitude	Longitude
;
DATALINES;
1049	1995	34.289001	-85.970065
1049	1999	34.289001	-85.970065
1073	1990	33.386389	-86.816667
1073	1995	33.331111	-87.003611
1073	1995	33.386389	-86.816667
1073	1995	33.578333	-86.773889
1073	1996	33.331111	-87.003611
1073	1996	33.386389	-86.816667
1073	1996	33.704722	-86.669167
1073	1996	33.578333	-86.773889
1073	1998	33.485556	-86.915
1073	1998	33.386389	-86.816667
1073	1999	33.331111	-87.003611
1073	1999	33.386389	-86.816667
;
RUN;

data temp;
   set have;
   by id year;
   retain group;
   if first.year then group=1;
   else group+1;
run;

proc transpose data=temp out=trans1 (drop=_name_)
     prefix=Latitude;
   by id year;
   id group;
   var Latitude;
run;
proc transpose data=temp out=trans2 (drop=_name_)
     prefix=Longitude;
   by id year;
   id group;
   var Longitude;
run;

data junk;
   merge trans1 trans2;
   by id year;
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 09 Aug 2021 01:17:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-transpose-and-rename-variables/m-p/760286#M240401</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-08-09T01:17:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to transpose and rename variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-transpose-and-rename-variables/m-p/760297#M240404</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;PRE&gt;data junk;&lt;/PRE&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Is that a subtle hint? &lt;span class="lia-unicode-emoji" title=":beaming_face_with_smiling_eyes:"&gt;😁&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Agree with you btw, the requested data format is unwieldy.&lt;/P&gt;
&lt;P&gt;Except as a final output (i.e. a report), data is -with very few exceptions- better kept in a long -rather than wide- format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Aug 2021 01:51:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-transpose-and-rename-variables/m-p/760297#M240404</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-08-09T01:51:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to transpose and rename variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-transpose-and-rename-variables/m-p/760298#M240405</link>
      <description>&lt;P&gt;So when you have multiple observations for a BY group and multiple variables then PROC TRANSPOSE will generate multiple variables and multiple observations.&amp;nbsp; Essentially it is transposing an NxM matrix to a MxN matrix.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could make it work with just PROC TRANPOSE by transposing three times.&amp;nbsp; Print the intermediate datasets to see what it happening.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have out=step1 name=variable prefix=_;
  by id year;
  var latitude longitude ;
run;

proc transpose date=step1 out=step2 prefix=_;
  by id year variable;
run;

proc transpose data=step2 out=want (drop=_name_);
  by id year ;
  id variable _name_;
  var _1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But it is probably easier (and definitely faster if the file is large) to just use arrays to transpose the data for you.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do col=1 by 1 until (last.year);
    set have (rename=(latitude=x longitude=y));
    by id year;
    array latitude [4];
    array longitude [4];
    latitude[col]=x;
    longitude[col]=y;
  end;
  drop col x y ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you don't know what size to use for the two array's then use an extra pass through the data to find maximum repetition into a macro variable and use the macro variable when defining the array.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select max(n) format=32. into :n trimmed
from (select id,year,count(*) as n from have group by id,year)
;
quit;

data want;
  do col=1 by 1 until (last.year);
    set have (rename=(latitude=x longitude=y));
    by id year;
    array latitude [&amp;amp;n];
    array longitude [&amp;amp;n];
    latitude[col]=x;
    longitude[col]=y;
  end;
  drop col x y ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 09 Aug 2021 01:57:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-transpose-and-rename-variables/m-p/760298#M240405</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-08-09T01:57:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to transpose and rename variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-transpose-and-rename-variables/m-p/760303#M240409</link>
      <description>Thanks Tom! This is a great solution too.</description>
      <pubDate>Mon, 09 Aug 2021 03:33:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-transpose-and-rename-variables/m-p/760303#M240409</guid>
      <dc:creator>elbarto</dc:creator>
      <dc:date>2021-08-09T03:33:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to transpose and rename variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-transpose-and-rename-variables/m-p/760354#M240435</link>
      <description>&lt;P&gt;The simplest way is using PROC SUMMARY.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA have;
input id	year	Latitude	Longitude
;
DATALINES;
1049	1995	34.289001	-85.970065
1049	1999	34.289001	-85.970065
1073	1990	33.386389	-86.816667
1073	1995	33.331111	-87.003611
1073	1995	33.386389	-86.816667
1073	1995	33.578333	-86.773889
1073	1996	33.331111	-87.003611
1073	1996	33.386389	-86.816667
1073	1996	33.704722	-86.669167
1073	1996	33.578333	-86.773889
1073	1998	33.485556	-86.915
1073	1998	33.386389	-86.816667
1073	1999	33.331111	-87.003611
1073	1999	33.386389	-86.816667
;
RUN;

proc sql noprint;
select max(n) into : n
 from (select count(*) as n from have group by id,year);
quit;
proc summary data=have;
by id year;
output out=want idgroup(out[&amp;amp;n] (Latitude	Longitude)=);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 09 Aug 2021 13:28:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-transpose-and-rename-variables/m-p/760354#M240435</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-08-09T13:28:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to transpose and rename variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-transpose-and-rename-variables/m-p/760466#M240476</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;BLOCKQUOTE&gt;
&lt;PRE&gt;data junk;&lt;/PRE&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Is that a subtle hint? &lt;span class="lia-unicode-emoji" title=":beaming_face_with_smiling_eyes:"&gt;😁&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Agree with you btw, the requested data format is unwieldy.&lt;/P&gt;
&lt;P&gt;Except as a final output (i.e. a report), data is -with very few exceptions- better kept in a long -rather than wide- format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In this case, yes the name of the data set is editorial.&lt;/P&gt;
&lt;P&gt;I have dealt with entirely too many "data sets" like that whose origins were the result of a report.&lt;/P&gt;
&lt;P&gt;I'm waiting for the follow-up request on using those lat/long pairs to draw something on a map. And then show how the existing data allows for polygons pretty cleanly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I often create a data set named junk for specific hopefully temporary cleaning steps. Like when a proc throws an unexpected value or two I'll get the associated records with a Where to create the Junk set. Then determine the "county" value I didn't expect in the output is because someone can't spell or transposed city and county or transposed digits in the Zip code or some other nonsense.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I ever were to need disk space then Junk (and Junk2 and Junk3 if present) are always allowed to be deleted.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Aug 2021 20:30:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-transpose-and-rename-variables/m-p/760466#M240476</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-08-09T20:30:50Z</dc:date>
    </item>
  </channel>
</rss>

