<?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: Creating new variables from long data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-from-long-data/m-p/502579#M134182</link>
    <description>&lt;P&gt;Here's a single data step solution that doesn't use do loops.&amp;nbsp; It makes use of the fact that your data appears to be sorted by study_id&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep=study_id hrz: act: drop=activity);
  set have;
  by study_id;
  array h {*} hrz1t hrz2t hrz3t;
  array a {*} act1t act2t act3t act4t;
  retain hrz1t--act4t;
  if first.id then call missing(of h{*},of a{*});
  h{hrzone}+1;
  a{activity}+1;
  if last.study_id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It assumes you have no missing values for activity or hrzone, and all the hrzone values are the integers 1, 2, or 3.&amp;nbsp; And activity is only the integers 1 through 4.&lt;/P&gt;</description>
    <pubDate>Tue, 09 Oct 2018 04:24:12 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2018-10-09T04:24:12Z</dc:date>
    <item>
      <title>Creating new variables from long data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-from-long-data/m-p/502510#M134150</link>
      <description>&lt;P&gt;I have a long dataset with 125 participants and 600,000 observations. I need to create several new variables summarizing the data. I can summarize the data but am having trouble figuring out how to create the new variables without having to do it for each participant separately. The two variables I am using to create the new variables are activity type with 4 categories (1-4) and hrzone with three categories (1-3).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I need is the amount of time that participants spent in each activity in seconds, which is just a sum of all of their observations since each observation per participant is equal to 1 second in that activity. So I will need 4 new variables (ACT1T-ACT4T) for each participant.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then, I need to create three other&amp;nbsp;variables for amount of time participants spent in each HRzone (HRZ1T-HRZ3T).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I need is a&amp;nbsp;dataset that looks like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="76"&gt;
&lt;P&gt;Study_id&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="72"&gt;
&lt;P&gt;HRZ1T&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="99"&gt;
&lt;P&gt;HRZ2T&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="99"&gt;
&lt;P&gt;HRZ3T&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="99"&gt;
&lt;P&gt;ACT1T&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;ACT2T&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;ACT3T&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;ACT4T&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="76"&gt;
&lt;P&gt;001&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="72"&gt;
&lt;P&gt;2300&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="99"&gt;
&lt;P&gt;1000&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="99"&gt;
&lt;P&gt;1500&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="99"&gt;
&lt;P&gt;2300&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;1000&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;5000&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;500&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="76"&gt;
&lt;P&gt;002&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="72"&gt;
&lt;P&gt;2200&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="99"&gt;
&lt;P&gt;800&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="99"&gt;
&lt;P&gt;700&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="99"&gt;
&lt;P&gt;2000&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;500&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;1000&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;200&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="76"&gt;
&lt;P&gt;003&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="72"&gt;
&lt;P&gt;2300&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="99"&gt;
&lt;P&gt;1000&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="99"&gt;
&lt;P&gt;1500&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="99"&gt;
&lt;P&gt;2300&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;1000&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;5000&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;500&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="76"&gt;
&lt;P&gt;004&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="72"&gt;
&lt;P&gt;2200&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="99"&gt;
&lt;P&gt;800&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="99"&gt;
&lt;P&gt;700&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="99"&gt;
&lt;P&gt;2000&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;500&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;1000&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;200&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've attached a very abbreviated mock dataset that shows my long data. In the original data I have 125 participants with between 1800 and 9005 observations each.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can run the code below for each participant to get their hrzone in each activity (hrz1t-hrz3t), but I would have to do that for all participants.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt; &lt;STRONG&gt;sql&lt;/STRONG&gt;&lt;SPAN&gt;;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;select&lt;/SPAN&gt; count(*) &lt;SPAN&gt;as&lt;/SPAN&gt; N_obs&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;from&lt;/SPAN&gt; mod_data&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;where&lt;/SPAN&gt; activity=&lt;SPAN&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN&gt;and&lt;/SPAN&gt; hrzone=&lt;SPAN&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/SPAN&gt;;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;&lt;SPAN&gt;;&amp;nbsp;&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 can also&amp;nbsp;just run a proc freq on activity for each participant to get the amount of time (number of observations) they spent in each activity.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN&gt;&lt;STRONG&gt;freq&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN&gt;data&lt;/SPAN&gt;=mod_data;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;where&lt;/SPAN&gt; study_id=&lt;SPAN&gt;"001"&lt;/SPAN&gt;;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;tables&lt;/SPAN&gt; activity;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;SPAN&gt;;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;What I need to be able to do create a loop or something similar to do it for all participants at the same time and to have it create a new dataset with 1 observation per participant like I have above so I can combine that data with the rest of the participants demographic data to do analyses.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I am not good with do loops and these types of things in general so any ideas to get this done would be apprecaited. Let me know if you need additional information.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Thank you in&amp;nbsp;advance.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Oct 2018 19:49:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-from-long-data/m-p/502510#M134150</guid>
      <dc:creator>rfarmenta</dc:creator>
      <dc:date>2018-10-08T19:49:55Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables from long data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-from-long-data/m-p/502517#M134153</link>
      <description>&lt;P&gt;I prefer to use by group processing when dealing with data like this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options MSGLEVEL=I;

/** Import an XLSX file.  **/

PROC IMPORT DATAFILE="/folders/myfolders/SAS Communities/mod_data.xlsx"
		    OUT=mod_data
		    DBMS=XLSX
		    REPLACE;
RUN;

data mod_data;
  set mod_data;
  indicator=1;
run;

* summarize hrzone data by participant ;

proc means data=mod_data sum noprint;
  var indicator;
  class study_id hrzone;
  types study_id*hrzone;
  output out=hrzone_summary sum=hrz;
run;

* reshape summarized hrzone data ;

proc transpose data=hrzone_summary(drop=_type_ _freq_)
               out=transposed_hrzone(drop=hrzone)
               name=hrzone prefix=HRZ
               suffix=T;
  by study_id;
  var hrz;
run;
 
* summarize activity data by participant ;

proc means data=mod_data sum noprint;
  var indicator;
  class study_id activity;
  types study_id*activity;
  output out=activity_summary sum=act;
run;

* reshape summarized activity data ;

proc transpose data=activity_summary(drop=_type_ _freq_)
               out=transposed_activity(drop=activity)
               name=activity prefix=ACT
               suffix=T;
  by study_id;
  var act;
run;


data results;
  merge transposed_hrzone
        transposed_activity;
  by study_id;
run;

proc print data=results noobs;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The screen shot below shows the output from the example code and example data.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Heart rate zone and activity data summarized by participant" style="width: 483px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/23871i09BF817942F12D1D/image-size/large?v=v2&amp;amp;px=999" role="button" title="Summarized heart rate zone and activity data participant.jpg" alt="Heart rate zone and activity data summarized by participant" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Heart rate zone and activity data summarized by participant&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Oct 2018 20:46:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-from-long-data/m-p/502517#M134153</guid>
      <dc:creator>SuzanneDorinski</dc:creator>
      <dc:date>2018-10-08T20:46:32Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables from long data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-from-long-data/m-p/502522#M134154</link>
      <description>&lt;P&gt;Since all of your categories are numeric why not just use PROC MEANS (summary) and PROC TRANSPOSE.&lt;/P&gt;
&lt;P&gt;Let's get some of your sample data in a usable format.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input study_ID Time HR Activity Age maxhrper hrzone;
cards;
1 0 114 4 27 60.96256684 1
1 1 114 4 27 60.96256684 1
1 2 101 4 27 54.01069519 1
1 3 95 4 27 50.80213904 1
1 4 92 4 27 49.19786096 3
1 5 93 4 27 49.73262032 3
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So first summarize by each of the different variables (ACTIVITY and HRZONE).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=have noprint ;
  by study_id;
  class activity hrzone ;
  ways 1 ;
  output out=summary N=N;
run;
proc print;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now let's pull out the variable name of the original class variables and use that for naming our new variable and then use PROC TRANSPOSE to convert .&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tall ;
  set summary ;
  array class activity hrzone ;
  length _name_ $32 value 8;
  value = coalesce(of class(*));
  _name_ = cats(vname(class(whichn(value,of class(*)))),value);
run;

proc print;
run;

proc transpose data=tall out=want ;
  by study_id;
  id _name_ ;
  var  N;
run;

proc print;
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: 360px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/23872i6ED0215011EB7BDF/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, 08 Oct 2018 20:57:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-from-long-data/m-p/502522#M134154</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-10-08T20:57:50Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables from long data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-from-long-data/m-p/502579#M134182</link>
      <description>&lt;P&gt;Here's a single data step solution that doesn't use do loops.&amp;nbsp; It makes use of the fact that your data appears to be sorted by study_id&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep=study_id hrz: act: drop=activity);
  set have;
  by study_id;
  array h {*} hrz1t hrz2t hrz3t;
  array a {*} act1t act2t act3t act4t;
  retain hrz1t--act4t;
  if first.id then call missing(of h{*},of a{*});
  h{hrzone}+1;
  a{activity}+1;
  if last.study_id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It assumes you have no missing values for activity or hrzone, and all the hrzone values are the integers 1, 2, or 3.&amp;nbsp; And activity is only the integers 1 through 4.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Oct 2018 04:24:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-from-long-data/m-p/502579#M134182</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-10-09T04:24:12Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables from long data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-from-long-data/m-p/502617#M134196</link>
      <description>&lt;P&gt;I would probably do something like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by study_id;
  array HRZ(*) 8 HRZ1T HRZ2T HRZ3T;
  array ACT(*) 8 ACT1T ACT2T ACT3T ACT4T;
  if first.study_id then 
    call missing(of HRZ(*),of ACT(*));
  HRZ(hrzone)+1;
  ACT(activity)+1;
  if last.study_id;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But you are making things difficult for yourself by using names with a suffixed T. It is much easier to refer to multiple variables if they have the number as a suffix, e.g.:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by study_id;
  array HRZ(*) 8 HRZ_time1-HRX_time3;
  array ACT(*) 8 ACT_time1-ACT_time4;
  if first.study_id then 
    call missing(of HRZ(*),of ACT(*));
  HRZ(hrzone)+1;
  ACT(activity)+1;
  if last.study_id;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;In this case it does not matter so much, but it will save you an awful lot of typing if you have a large number of categories.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Oct 2018 08:25:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-from-long-data/m-p/502617#M134196</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-10-09T08:25:15Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables from long data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-from-long-data/m-p/503841#M134716</link>
      <description>&lt;P&gt;This worked well and gave what I am looking for overall. I am now trying to do something similar where I am trying to create new variables that summarizes the amount of time in hrzone by activity level for each participant. So essentially I need to create 18 new variables that would look like this: if activity=1 and hrzone=1 then acthr11=XX, if activity=2 and hrzone=2 then acthr12=XX...and so on.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I thought I could modify the code like this:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN&gt;&lt;STRONG&gt;means&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN&gt;data&lt;/SPAN&gt;=mod_data &lt;SPAN&gt;sum&lt;/SPAN&gt; &lt;SPAN&gt;noprint&lt;/SPAN&gt;;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;var&lt;/SPAN&gt; indicator;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;class&lt;/SPAN&gt; study_id activity hrzone;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;types&lt;/SPAN&gt; study_id*HRZONE*activity;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;output&lt;/SPAN&gt; &lt;SPAN&gt;out&lt;/SPAN&gt;=acthr_summary &lt;SPAN&gt;sum&lt;/SPAN&gt;=acthr;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;SPAN&gt;;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;to create those&amp;nbsp;variables and then transpose the data using the same code:&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN&gt;&lt;STRONG&gt;transpose&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN&gt;data&lt;/SPAN&gt;=acthr_summary(drop=_type_ _freq_)&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;out&lt;/SPAN&gt;=transposed_acthr(drop=activity)&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;name&lt;/SPAN&gt;=activity &lt;SPAN&gt;prefix&lt;/SPAN&gt;=ACTHR&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;suffix&lt;/SPAN&gt;=T;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;by&lt;/SPAN&gt; study_id;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;var&lt;/SPAN&gt; acthr;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;SPAN&gt;;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;That seems to create the right information&amp;nbsp;where I get new variables with the amount of seconds for each person for&amp;nbsp;hrzone*activity, however,&amp;nbsp;the data does not seem to appear in the right variables for&amp;nbsp;each person. IF you look at the output I have&amp;nbsp;from the data you will see two participants, for participant 001, they participated in 3 activities and went into multiple HR zones. Those are shown under&amp;nbsp;certain variable names. For participant 002, they participated in more activities and had multiple HR zones. But the way the data is transposed put different activity* hrzones under the same variable name as it did for for a different combo for the first participant. I am sure there is a way to reconcile that but I am not quite sure how. I hope this makes sense.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Oct 2018 18:56:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-from-long-data/m-p/503841#M134716</guid>
      <dc:creator>rfarmenta</dc:creator>
      <dc:date>2018-10-12T18:56:43Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables from long data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-from-long-data/m-p/503926#M134768</link>
      <description>&lt;P&gt;I wasn't able to download the PDFs that you provided.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The following bit of code may be useful.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=mod_data n completetypes;
  var indicator;
  class study_id activity hrzone;
  types study_id*activity*hrzone;
  output out=acthr_summary n=acthr;
  title 'Time in activity by hrzone level for each participant';
run;

* concatenate 'ACTHR', activity, hrzone, and 'T' to use when ;
* naming variable after transpose ;

data acthr_summary_with_combo;
  length combo $ 8;
  set acthr_summary(drop=_type_ _freq_);
  combo=cat('ACTHR',activity,hrzone,'T');
run;

proc transpose data=acthr_summary_with_combo(drop=activity hrzone)
               out=transposed_acthr(drop=_name_);
  by study_id;
  var acthr;
  id combo;
run;

proc print data=transposed_acthr;
  title 'Transposed data for time in activity by hrzone';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The screen shot below shows part of the PROC MEANS output.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Partial PROC MEANS output for time in activity by hrzone for each study participant" style="width: 343px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/23984i6A027F5D5B57BF6A/image-size/large?v=v2&amp;amp;px=999" role="button" title="Partial screen shot of PROC MEANS output for time in activity by hrzone for each participant.jpg" alt="Partial PROC MEANS output for time in activity by hrzone for each study participant" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Partial PROC MEANS output for time in activity by hrzone for each study participant&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;The next screen shot shows the transposed data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Transposed data for time in activity by hrzone" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/23985iA77969DFD02902C0/image-size/large?v=v2&amp;amp;px=999" role="button" title="Transposed data for time in activity by hrzone for each participant.jpg" alt="Transposed data for time in activity by hrzone" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Transposed data for time in activity by hrzone&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Oct 2018 21:43:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-from-long-data/m-p/503926#M134768</guid>
      <dc:creator>SuzanneDorinski</dc:creator>
      <dc:date>2018-10-12T21:43:01Z</dc:date>
    </item>
  </channel>
</rss>

