<?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: how can i group variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-can-i-group-variables/m-p/860044#M339766</link>
    <description>&lt;P&gt;There's typically two methods to do this, one is to loop through the data and combine, the second is a transpose method. Because you're doing more than one variable, the data step approach is more efficient.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a worked example with fictional data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*create sample data for demonstration;
data have;
    infile cards truncover;
    input OrgID Product $   States $ NumVar;
    cards;
1   football    DC  54
1   football    VA  84
1   football    MD  38 	
2   football    CA  45
3   football    NV  58
3   football    CA  98
;
run;

*Sort - required for both options;
proc sort data=have;
    by orgID;
run;

**********************************************************************;
*Use RETAIN and BY group processing to combine the information;
**********************************************************************;
data want_option1;
    set have;
    by orgID;
    length combinedStates $100 combinedNumVar $100;
    retain combinedStates combinedNumVar;

    if first.orgID then do;
        combinedStates=states;
        combinedNumVar = put(numVar, 8. -l);
    end;
    else do;
        combinedStates=catx('/', combinedStates, states);
        combinedNumVar=catx('/', combinedNumVar,  put(numVar, 8. -l));
     end;
     
     
    if last.orgID then
        output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16220"&gt;@rykwong&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;hi, hope you can help me on this&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a large dataset that each line is showing image types of a MRI study.&amp;nbsp; Lines 1-4 are from 1 MRI study and lines 5-9 are from another MRI study.&amp;nbsp; So there are 2 MRI studies shown here, but my dataset contains a lot more MRI studies.&amp;nbsp; Note that the fields studydate, studydescription, operatorname contains only a single value specific to the MRI study; whereas the fields seriesnumber and seriesdescription contain data specific to the image types.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="rykwong_0-1677015803198.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/80684iB6403363F5008E07/image-size/medium?v=v2&amp;amp;px=400" role="button" title="rykwong_0-1677015803198.png" alt="rykwong_0-1677015803198.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;would like to shrink the dataset into each line represents a MRI study (so from this collapse into 2 lines), by:&lt;/P&gt;
&lt;P&gt;1) creating a new field that contains all the values of series number of an MRI, separated by a "/" (i.e. 1/2/3/4 for the first MRI study and 1/2/3/4/5 for the second study)&lt;/P&gt;
&lt;P&gt;2) creating a new field that contains all of the values of series description of that MRI separated by a "/" (i.e. localizer/3pl loc/.....bh chest)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 21 Feb 2023 22:58:40 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2023-02-21T22:58:40Z</dc:date>
    <item>
      <title>how can i group variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-can-i-group-variables/m-p/860030#M339756</link>
      <description>&lt;P&gt;hi, hope you can help me on this&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a large dataset that each line is showing image types of a MRI study.&amp;nbsp; Lines 1-4 are from 1 MRI study and lines 5-9 are from another MRI study.&amp;nbsp; So there are 2 MRI studies shown here, but my dataset contains a lot more MRI studies.&amp;nbsp; Note that the fields studydate, studydescription, operatorname contains only a single value specific to the MRI study; whereas the fields seriesnumber and seriesdescription contain data specific to the image types.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="rykwong_0-1677015803198.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/80684iB6403363F5008E07/image-size/medium?v=v2&amp;amp;px=400" role="button" title="rykwong_0-1677015803198.png" alt="rykwong_0-1677015803198.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;would like to shrink the dataset into each line represents a MRI study (so from this collapse into 2 lines), by:&lt;/P&gt;
&lt;P&gt;1) creating a new field that contains all the values of series number of an MRI, separated by a "/" (i.e. 1/2/3/4 for the first MRI study and 1/2/3/4/5 for the second study)&lt;/P&gt;
&lt;P&gt;2) creating a new field that contains all of the values of series description of that MRI separated by a "/" (i.e. localizer/3pl loc/.....bh chest)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Feb 2023 22:01:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-can-i-group-variables/m-p/860030#M339756</guid>
      <dc:creator>rykwong</dc:creator>
      <dc:date>2023-02-21T22:01:19Z</dc:date>
    </item>
    <item>
      <title>Re: how can i group variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-can-i-group-variables/m-p/860044#M339766</link>
      <description>&lt;P&gt;There's typically two methods to do this, one is to loop through the data and combine, the second is a transpose method. Because you're doing more than one variable, the data step approach is more efficient.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a worked example with fictional data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*create sample data for demonstration;
data have;
    infile cards truncover;
    input OrgID Product $   States $ NumVar;
    cards;
1   football    DC  54
1   football    VA  84
1   football    MD  38 	
2   football    CA  45
3   football    NV  58
3   football    CA  98
;
run;

*Sort - required for both options;
proc sort data=have;
    by orgID;
run;

**********************************************************************;
*Use RETAIN and BY group processing to combine the information;
**********************************************************************;
data want_option1;
    set have;
    by orgID;
    length combinedStates $100 combinedNumVar $100;
    retain combinedStates combinedNumVar;

    if first.orgID then do;
        combinedStates=states;
        combinedNumVar = put(numVar, 8. -l);
    end;
    else do;
        combinedStates=catx('/', combinedStates, states);
        combinedNumVar=catx('/', combinedNumVar,  put(numVar, 8. -l));
     end;
     
     
    if last.orgID then
        output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16220"&gt;@rykwong&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;hi, hope you can help me on this&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a large dataset that each line is showing image types of a MRI study.&amp;nbsp; Lines 1-4 are from 1 MRI study and lines 5-9 are from another MRI study.&amp;nbsp; So there are 2 MRI studies shown here, but my dataset contains a lot more MRI studies.&amp;nbsp; Note that the fields studydate, studydescription, operatorname contains only a single value specific to the MRI study; whereas the fields seriesnumber and seriesdescription contain data specific to the image types.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="rykwong_0-1677015803198.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/80684iB6403363F5008E07/image-size/medium?v=v2&amp;amp;px=400" role="button" title="rykwong_0-1677015803198.png" alt="rykwong_0-1677015803198.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;would like to shrink the dataset into each line represents a MRI study (so from this collapse into 2 lines), by:&lt;/P&gt;
&lt;P&gt;1) creating a new field that contains all the values of series number of an MRI, separated by a "/" (i.e. 1/2/3/4 for the first MRI study and 1/2/3/4/5 for the second study)&lt;/P&gt;
&lt;P&gt;2) creating a new field that contains all of the values of series description of that MRI separated by a "/" (i.e. localizer/3pl loc/.....bh chest)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Feb 2023 22:58:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-can-i-group-variables/m-p/860044#M339766</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-02-21T22:58:40Z</dc:date>
    </item>
    <item>
      <title>Re: how can i group variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-can-i-group-variables/m-p/860045#M339767</link>
      <description>thanks so much, I will try this I think this will work.  May I ask you the meaning of the "-1" in teh put(numvar, 8. -1)?&lt;BR /&gt;</description>
      <pubDate>Tue, 21 Feb 2023 23:23:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-can-i-group-variables/m-p/860045#M339767</guid>
      <dc:creator>rykwong</dc:creator>
      <dc:date>2023-02-21T23:23:39Z</dc:date>
    </item>
    <item>
      <title>Re: how can i group variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-can-i-group-variables/m-p/860052#M339770</link>
      <description>&lt;P&gt;There is an &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n0mlfb88dkhbmun1x08qbh5xbs7e.htm" target="_self"&gt;optional parameter in the PUT function that controls the alignment of the text&lt;/A&gt;. &lt;BR /&gt;&lt;BR /&gt;That is actually a dash and the letter L. It left aligns the text so there are no extra spaces added in to the text.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Feb 2023 23:48:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-can-i-group-variables/m-p/860052#M339770</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-02-21T23:48:01Z</dc:date>
    </item>
    <item>
      <title>Re: how can i group variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-can-i-group-variables/m-p/860204#M339842</link>
      <description>wonderful &lt;BR /&gt;it works great!  much appreciated&lt;BR /&gt;</description>
      <pubDate>Wed, 22 Feb 2023 15:48:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-can-i-group-variables/m-p/860204#M339842</guid>
      <dc:creator>rykwong</dc:creator>
      <dc:date>2023-02-22T15:48:15Z</dc:date>
    </item>
  </channel>
</rss>

