<?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: transpose long data to wide while keeping numeric data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/transpose-long-data-to-wide-while-keeping-numeric-data/m-p/784338#M250293</link>
    <description>&lt;P&gt;I'm showing un-transposed. I wanted new columns with like ht_baseline, ht_year_2, bp_baseline, bp_year_2, etc.&amp;nbsp; But the transpose code i originally posted changes all numeric vars to character.&lt;/P&gt;</description>
    <pubDate>Mon, 06 Dec 2021 16:11:32 GMT</pubDate>
    <dc:creator>eawhit10</dc:creator>
    <dc:date>2021-12-06T16:11:32Z</dc:date>
    <item>
      <title>transpose long data to wide while keeping numeric data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/transpose-long-data-to-wide-while-keeping-numeric-data/m-p/784316#M250278</link>
      <description>&lt;P&gt;I have a rather large dataset in long format that I need wide. I tried proc transpose, but all my numeric variables were changed to character. 1) I don't even know how to change them back and don't want to do so individually anyway as there are a lot of variables 2) is there a way to transpose without this happening?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please see data (in reality there are about &lt;STRONG&gt;63 variables&lt;/STRONG&gt;, character and numeric, like vascular measures,&amp;nbsp; yes/no questions, more physical measures, etc). Any solution I find involving an array seems to require typing out all variables....&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;event_name&lt;/TD&gt;&lt;TD&gt;stid&lt;/TD&gt;&lt;TD&gt;income&lt;/TD&gt;&lt;TD&gt;educ&lt;/TD&gt;&lt;TD&gt;sex&lt;/TD&gt;&lt;TD&gt;race&lt;/TD&gt;&lt;TD&gt;healthp&lt;/TD&gt;&lt;TD&gt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lt;TD&gt;200&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;62.5&lt;/TD&gt;&lt;TD&gt;125.8&lt;/TD&gt;&lt;TD&gt;120&lt;/TD&gt;&lt;TD&gt;86&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;baseline&lt;/TD&gt;&lt;TD&gt;300&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;64&lt;/TD&gt;&lt;TD&gt;150.1&lt;/TD&gt;&lt;TD&gt;129&lt;/TD&gt;&lt;TD&gt;77&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;year_2&lt;/TD&gt;&lt;TD&gt;300&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;64&lt;/TD&gt;&lt;TD&gt;145.6&lt;/TD&gt;&lt;TD&gt;114&lt;/TD&gt;&lt;TD&gt;74&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;/* trying proc transpose*/&lt;/P&gt;&lt;P&gt;proc transpose data=longsort out=out1;&lt;BR /&gt;by stID event_name;&amp;nbsp;&lt;BR /&gt;var income -- dbp;&lt;BR /&gt;run;&lt;BR /&gt;proc transpose data=out1 delimiter=_ out=new2(drop=_name_);&lt;BR /&gt;by stid;&lt;BR /&gt;var col1;&lt;BR /&gt;id _name_ event_name;&lt;BR /&gt;run;&lt;BR /&gt;/* proc contents shows all are now character variables even the ones supposed to be numeric */&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Dec 2021 14:18:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/transpose-long-data-to-wide-while-keeping-numeric-data/m-p/784316#M250278</guid>
      <dc:creator>eawhit10</dc:creator>
      <dc:date>2021-12-06T14:18:19Z</dc:date>
    </item>
    <item>
      <title>Re: transpose long data to wide while keeping numeric data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/transpose-long-data-to-wide-while-keeping-numeric-data/m-p/784320#M250281</link>
      <description>&lt;P&gt;Are you showing us the transposed data or the un-transposed data?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;I have a rather large dataset in long format that I need wide&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Usually converting long to wide is not a good idea, although there are exceptions. Why do you need it to be wide?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Dec 2021 14:47:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/transpose-long-data-to-wide-while-keeping-numeric-data/m-p/784320#M250281</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-12-06T14:47:44Z</dc:date>
    </item>
    <item>
      <title>Re: transpose long data to wide while keeping numeric data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/transpose-long-data-to-wide-while-keeping-numeric-data/m-p/784322#M250282</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;var firstvar-character-lastvar;
var firstvar-numeric-lastvar;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;One TRANSPOSE for character and another for numeric.

Read up on "SAS Variable Lists".&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Dec 2021 14:53:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/transpose-long-data-to-wide-while-keeping-numeric-data/m-p/784322#M250282</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2021-12-06T14:53:05Z</dc:date>
    </item>
    <item>
      <title>Re: transpose long data to wide while keeping numeric data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/transpose-long-data-to-wide-while-keeping-numeric-data/m-p/784326#M250285</link>
      <description>&lt;P&gt;But you are already starting with a WIDE dataset.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is it you want to create?&amp;nbsp; Something WIDER?&amp;nbsp; How?&lt;/P&gt;</description>
      <pubDate>Mon, 06 Dec 2021 15:29:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/transpose-long-data-to-wide-while-keeping-numeric-data/m-p/784326#M250285</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-12-06T15:29:39Z</dc:date>
    </item>
    <item>
      <title>Re: transpose long data to wide while keeping numeric data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/transpose-long-data-to-wide-while-keeping-numeric-data/m-p/784330#M250286</link>
      <description>&lt;P&gt;So to go from semi-wide to really wide you can use a double transpose.&amp;nbsp; If you have mixed numeric/character data then do it in two part and then merge them.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input event_name :$32. stid $ income $ educ $ sex $ race $ healthp $ ht wt sbp dbp smoke $;
cards;
baseline 100 1 2 1 1 5 66 140.1 144 98 1
year_2 100 1 2 1 1 4 66 150.1 144 98 1
baseline 200 3 3 2 4 3 62.5 130.2 121 73 2
year_2 200 3 3 2 4 2 62.5 125.8 120 86 2
baseline 300 1 4 1 2 1 64 150.1 129 77 2
year_2 300 1 4 1 2 3 64 145.6 114 74 2
;
proc sort data=have;
  by stid event_name;
run;

proc transpose data=have out=num;
  by stid event_name ; 
  var _numeric_;
run;
proc transpose data=num out=num_wide delimiter=_;
  by stid ;
  where lowcase(_name_) not in ('stid'  'event_name');
  id _name_ event_name;
  var col1;
run;

proc transpose data=have out=char;
  by stid event_name ; 
  var _character_;
run;
proc transpose data=char out=char_wide delimiter=_;
  by stid ;
  where lowcase(_name_) not in ('stid'  'event_name');
  id _name_ event_name;
  var col1;
run;

data want;
  merge num_wide char_wide ;
  by stid ; 
  drop _name_;
run;&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: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66438iCEAE7DF3C9B2AFAD/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>Mon, 06 Dec 2021 15:42:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/transpose-long-data-to-wide-while-keeping-numeric-data/m-p/784330#M250286</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-12-06T15:42:06Z</dc:date>
    </item>
    <item>
      <title>Re: transpose long data to wide while keeping numeric data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/transpose-long-data-to-wide-while-keeping-numeric-data/m-p/784337#M250292</link>
      <description>&lt;P&gt;I thought that in order to run some longitudinal tests, I needed it in wide data? A simple example would be like to compare weight from baseline and year 2, I'd need 2 columns, instead of observation at baseline in 1 row and row 2 is the observation at year 2.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Dec 2021 16:09:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/transpose-long-data-to-wide-while-keeping-numeric-data/m-p/784337#M250292</guid>
      <dc:creator>eawhit10</dc:creator>
      <dc:date>2021-12-06T16:09:36Z</dc:date>
    </item>
    <item>
      <title>Re: transpose long data to wide while keeping numeric data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/transpose-long-data-to-wide-while-keeping-numeric-data/m-p/784338#M250293</link>
      <description>&lt;P&gt;I'm showing un-transposed. I wanted new columns with like ht_baseline, ht_year_2, bp_baseline, bp_year_2, etc.&amp;nbsp; But the transpose code i originally posted changes all numeric vars to character.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Dec 2021 16:11:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/transpose-long-data-to-wide-while-keeping-numeric-data/m-p/784338#M250293</guid>
      <dc:creator>eawhit10</dc:creator>
      <dc:date>2021-12-06T16:11:32Z</dc:date>
    </item>
    <item>
      <title>Re: transpose long data to wide while keeping numeric data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/transpose-long-data-to-wide-while-keeping-numeric-data/m-p/784343#M250296</link>
      <description>&lt;P&gt;Thank you! This is what I was looking for. I know it seems excessively wide, but I didn't know how else to prepare the data to compare baseline measures vs year 2.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Dec 2021 16:39:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/transpose-long-data-to-wide-while-keeping-numeric-data/m-p/784343#M250296</guid>
      <dc:creator>eawhit10</dc:creator>
      <dc:date>2021-12-06T16:39:17Z</dc:date>
    </item>
    <item>
      <title>Re: transpose long data to wide while keeping numeric data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/transpose-long-data-to-wide-while-keeping-numeric-data/m-p/784345#M250297</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/358529"&gt;@eawhit10&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you! This is what I was looking for. I know it seems excessively wide, but I didn't know how else to prepare the data to compare baseline measures vs year 2.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Change from baseline calculations are easier in the original format.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by id;
  if visit='Baseline' then wt_baseline=wt;
  if first.id the call missing(wt_baseline);
  retain wt_baseline;
  if not missing(wt_baseline) then wt_change=wt-wt_baseline;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 06 Dec 2021 17:08:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/transpose-long-data-to-wide-while-keeping-numeric-data/m-p/784345#M250297</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-12-06T17:08:54Z</dc:date>
    </item>
    <item>
      <title>Re: transpose long data to wide while keeping numeric data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/transpose-long-data-to-wide-while-keeping-numeric-data/m-p/784507#M250371</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input event_name :$32. stid $ income $ educ $ sex $ race $ healthp $ ht wt sbp dbp smoke $;
cards;
baseline 100 1 2 1 1 5 66 140.1 144 98 1
year_2 100 1 2 1 1 4 66 150.1 144 98 1
baseline 200 3 3 2 4 3 62.5 130.2 121 73 2
year_2 200 3 3 2 4 2 62.5 125.8 120 86 2
baseline 300 1 4 1 2 1 64 150.1 129 77 2
year_2 300 1 4 1 2 3 64 145.6 114 74 2
;
proc sort data=have;
  by stid event_name;
run;
proc transpose data=have(obs=0) out=vname;
var _all_;
run;
proc sql;
create table level as
select *
 from (select distinct event_name from have),
 (select * from vname where lowcase(_name_) not in ('stid'  'event_name'))
 order by 1;
quit;
data _null_;
 set level end=last;
 by event_name;
 if _n_=1 then call execute('data want;merge ');
 if first.event_name then call execute(catt('have(where=(event_name="',event_name,'")  rename=('));
 call execute(catt(_name_,'=',_name_,'_',event_name));
 if last.event_name then call execute('))');
 if last then call execute(';by stid;drop event_name;run;');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 07 Dec 2021 11:44:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/transpose-long-data-to-wide-while-keeping-numeric-data/m-p/784507#M250371</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-12-07T11:44:18Z</dc:date>
    </item>
  </channel>
</rss>

