<?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 to Group variables with same suffix and different prefix in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-Group-variables-with-same-suffix-and-different-prefix/m-p/903934#M357144</link>
    <description>&lt;P&gt;I agree with the suggestion to transpose this data into a more vertical / normalized format to make it easier to work with.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/438491"&gt;@JMagenta&lt;/a&gt;&amp;nbsp;, could you post some example data?&amp;nbsp; Ideally, a DATA step that uses the CARDS statement with just a few rows and a few variables, maybe ID Apple1-Apple3 Carrot1-Carrot3 Orange1-Orange3?&amp;nbsp; Also please show the output data you would want for this example data.&lt;/P&gt;</description>
    <pubDate>Mon, 20 Nov 2023 15:39:37 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2023-11-20T15:39:37Z</dc:date>
    <item>
      <title>How to Group variables with same suffix and different prefix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Group-variables-with-same-suffix-and-different-prefix/m-p/903811#M357111</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I have a problem. I have 60 variables:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Apples1--Apples20&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Carrots1--Carrots20&lt;/P&gt;
&lt;P&gt;Oranges1--Oranges20&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I want to do is group variables with the same suffix together in the SAS dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Apples1 Carrots1 Oranges1 ... Apples20 Carrots20 Oranges20&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How can I do this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Next, depending on the value of Apples1--Apples20 ( using formula Min(Apples1--Apples20) to find the minimum value, I would like to create new variables with their values.&lt;/P&gt;
&lt;P&gt;For instance:&lt;/P&gt;
&lt;P&gt;Apples2 is less than all in group Apples1--Apples20, so :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Earlyfruit=Apples2;&lt;/P&gt;
&lt;P&gt;Afruit=Carrot2;&lt;/P&gt;
&lt;P&gt;Bfruit=Orange2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is this possible? Can I use "proc sql"&amp;nbsp; to accomplish this somehow?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any advice would be helpful.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;P&gt;J Magenta&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Nov 2023 16:57:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Group-variables-with-same-suffix-and-different-prefix/m-p/903811#M357111</guid>
      <dc:creator>JMagenta</dc:creator>
      <dc:date>2023-11-19T16:57:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to Group variables with same suffix and different prefix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Group-variables-with-same-suffix-and-different-prefix/m-p/903834#M357122</link>
      <description>&lt;P&gt;Here you go.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I assume some of the SAS syntax used will be new to you. I suggest you first consult the SAS docu and then ask questions for the bits that remain unclear.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/***
I have 60 variables:
Apples1--Apples20 
Carrots1--Carrots20
Oranges1--Oranges20
**/
data work.have;
  array Apples  {20} 8;
  array Carrots {20} 8;
  array Oranges {20} 8;
  do i=1 to dim(apples);
    apples[i]=mod(i*10,dim(apples))+1;
    carrots[i]=apples[i]*5;
    oranges[i]=apples[i]*8;
  end;
  drop i;
run;

/* 1. What I want to do is group variables with the same suffix together in the SAS dataset */
/* Comment: The order of variables in a table is of no real relevance. Such ordering requests are normally reporting */ 
/*          requirements to be addressed as part of report generation                                                */

/* sort selected variables by digits in the variable name and populate a macro variable with the result */
proc sql noprint;
  select name into :varlist separated by ' '
  from dictionary.columns
  where 
    libname='WORK' 
    and memname='HAVE'
    and (
             upcase(name) like 'APPLES%'
          or upcase(name) like 'CARROTS%'
          or upcase(name) like 'ORANGES%'
        )
  order by compress(name,,'kd'), name
  ;
quit;

/* use macro variable to print data */
proc print data=work.have;
  var &amp;amp;varlist;
run;

/* 2. Next, depending on the value of Apples1--Apples20 ( using formula Min(Apples1--Apples20) */
/*    to find the minimum value, I would like to create new variables with their values.       */

/* below 3 coding options for doing this.                                                                                     */
/* - option one - of Apples: - will include all variables where the name starts with Apple - would also include AppleComputer */
data want;
  set have;
  array _oranges{*} Oranges1 - Oranges20;
  min_apple=min(of Apples:);
  min_carrots=min(of Carrots1 - Carrots20);
  min_oranges=min(of _oranges[*]);
run;

proc print data=want;
  var min_:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1700451514914.png" style="width: 738px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/90000i102F0695FAE724D6/image-dimensions/738x141?v=v2" width="738" height="141" role="button" title="Patrick_0-1700451514914.png" alt="Patrick_0-1700451514914.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here the chatGPT explanation of above code which isn't too bad&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;OL&gt;
&lt;LI&gt;
&lt;P&gt;&lt;STRONG&gt;Creating the Initial Dataset (&lt;CODE&gt;work.have&lt;/CODE&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;DIV class="bg-black rounded-md"&gt;
&lt;DIV class="flex items-center relative text-gray-200 bg-gray-800 gizmo:dark:bg-token-surface-primary px-4 py-2 text-xs font-sans justify-between rounded-t-md"&gt;&lt;SPAN&gt;sas&lt;/SPAN&gt;&lt;BUTTON class="flex ml-auto gizmo:ml-0 gap-1 items-center"&gt;Copy code&lt;/BUTTON&gt;&lt;/DIV&gt;
&lt;DIV class="p-4 overflow-y-auto"&gt;&lt;CODE class="!whitespace-pre hljs language-sas"&gt;data work.have;
  array Apples  {20} 8;
  array Carrots {20} 8;
  array Oranges {20} 8;
  do i=1 to dim(apples);
    apples[i]=mod(i*10,dim(apples))+1;
    carrots[i]=apples[i]*5;
    oranges[i]=apples[i]*8;
  end;
  drop i;
run;
&lt;/CODE&gt;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;UL&gt;
&lt;LI&gt;This part of the code creates a dataset named &lt;CODE&gt;work.have&lt;/CODE&gt; with 60 variables: &lt;CODE&gt;Apples1&lt;/CODE&gt; through &lt;CODE&gt;Apples20&lt;/CODE&gt;, &lt;CODE&gt;Carrots1&lt;/CODE&gt; through &lt;CODE&gt;Carrots20&lt;/CODE&gt;, and &lt;CODE&gt;Oranges1&lt;/CODE&gt; through &lt;CODE&gt;Oranges20&lt;/CODE&gt;.&lt;/LI&gt;
&lt;LI&gt;Three arrays (&lt;CODE&gt;Apples&lt;/CODE&gt;, &lt;CODE&gt;Carrots&lt;/CODE&gt;, and &lt;CODE&gt;Oranges&lt;/CODE&gt;) of size 20 are defined.&lt;/LI&gt;
&lt;LI&gt;A loop is used to populate these arrays with values based on a formula (&lt;CODE&gt;apples[i]=mod(i*10,dim(apples))+1&lt;/CODE&gt;) and similar formulas for &lt;CODE&gt;carrots&lt;/CODE&gt; and &lt;CODE&gt;oranges&lt;/CODE&gt;.&lt;/LI&gt;
&lt;LI&gt;The &lt;CODE&gt;drop i;&lt;/CODE&gt; statement removes the loop index variable &lt;CODE&gt;i&lt;/CODE&gt;.&lt;/LI&gt;
&lt;/UL&gt;
&lt;/LI&gt;
&lt;LI&gt;
&lt;P&gt;&lt;STRONG&gt;Sorting Variables and Creating a Macro Variable (&lt;CODE&gt;varlist&lt;/CODE&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;DIV class="bg-black rounded-md"&gt;
&lt;DIV class="flex items-center relative text-gray-200 bg-gray-800 gizmo:dark:bg-token-surface-primary px-4 py-2 text-xs font-sans justify-between rounded-t-md"&gt;&lt;SPAN&gt;sas&lt;/SPAN&gt;&lt;BUTTON class="flex ml-auto gizmo:ml-0 gap-1 items-center"&gt;Copy code&lt;/BUTTON&gt;&lt;/DIV&gt;
&lt;DIV class="p-4 overflow-y-auto"&gt;&lt;CODE class="!whitespace-pre hljs language-sas"&gt;proc sql noprint;
  select name into :varlist separated by ' '
  from dictionary.columns
  where 
    libname='WORK' 
    and memname='HAVE'
    and (
             upcase(name) like 'APPLES%'
          or upcase(name) like 'CARROTS%'
          or upcase(name) like 'ORANGES%'
        )
  order by compress(name,,'kd'), name
  ;
quit;
&lt;/CODE&gt;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;UL&gt;
&lt;LI&gt;This part of the code uses PROC SQL to query the &lt;CODE&gt;dictionary.columns&lt;/CODE&gt; table to get the variable names in the &lt;CODE&gt;work.have&lt;/CODE&gt; dataset that meet certain conditions.&lt;/LI&gt;
&lt;LI&gt;The conditions are specified in the &lt;CODE&gt;where&lt;/CODE&gt; clause, filtering for variables starting with 'APPLES', 'CARROTS', or 'ORANGES'.&lt;/LI&gt;
&lt;LI&gt;The result is sorted by the numerical part of the variable names (&lt;CODE&gt;order by compress(name,,'kd'), name&lt;/CODE&gt;).&lt;/LI&gt;
&lt;LI&gt;The selected variable names are stored in a macro variable named &lt;CODE&gt;varlist&lt;/CODE&gt;, separated by spaces.&lt;/LI&gt;
&lt;/UL&gt;
&lt;/LI&gt;
&lt;LI&gt;
&lt;P&gt;&lt;STRONG&gt;Printing Selected Variables Using Macro Variable:&lt;/STRONG&gt;&lt;/P&gt;
&lt;DIV class="bg-black rounded-md"&gt;
&lt;DIV class="flex items-center relative text-gray-200 bg-gray-800 gizmo:dark:bg-token-surface-primary px-4 py-2 text-xs font-sans justify-between rounded-t-md"&gt;&lt;SPAN&gt;sas&lt;/SPAN&gt;&lt;BUTTON class="flex ml-auto gizmo:ml-0 gap-1 items-center"&gt;Copy code&lt;/BUTTON&gt;&lt;/DIV&gt;
&lt;DIV class="p-4 overflow-y-auto"&gt;&lt;CODE class="!whitespace-pre hljs language-sas"&gt;proc print data=work.have;
  var &amp;amp;varlist;
run;
&lt;/CODE&gt;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;UL&gt;
&lt;LI&gt;This part of the code prints the &lt;CODE&gt;work.have&lt;/CODE&gt; dataset, selecting the variables specified in the macro variable &lt;CODE&gt;varlist&lt;/CODE&gt;.&lt;/LI&gt;
&lt;LI&gt;The &lt;CODE&gt;&amp;amp;varlist&lt;/CODE&gt; is resolved at runtime, and it includes the list of variables retrieved and sorted in the previous step.&lt;/LI&gt;
&lt;/UL&gt;
&lt;/LI&gt;
&lt;LI&gt;
&lt;P&gt;&lt;STRONG&gt;Calculating Minimum Values and Creating a New Dataset (&lt;CODE&gt;want&lt;/CODE&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;DIV class="bg-black rounded-md"&gt;
&lt;DIV class="flex items-center relative text-gray-200 bg-gray-800 gizmo:dark:bg-token-surface-primary px-4 py-2 text-xs font-sans justify-between rounded-t-md"&gt;&lt;SPAN&gt;sas&lt;/SPAN&gt;&lt;BUTTON class="flex ml-auto gizmo:ml-0 gap-1 items-center"&gt;Copy code&lt;/BUTTON&gt;&lt;/DIV&gt;
&lt;DIV class="p-4 overflow-y-auto"&gt;&lt;CODE class="!whitespace-pre hljs language-sas"&gt;data want;
  set have;
  array _oranges{*} Oranges1 - Oranges20;
  min_apple=min(of Apples:);
  min_carrots=min(of Carrots1 - Carrots20);
  min_oranges=min(of _oranges[*]);
run;
&lt;/CODE&gt;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;UL&gt;
&lt;LI&gt;This part of the code creates a new dataset named &lt;CODE&gt;want&lt;/CODE&gt; by using the &lt;CODE&gt;set&lt;/CODE&gt; statement to copy the observations from the &lt;CODE&gt;have&lt;/CODE&gt; dataset.&lt;/LI&gt;
&lt;LI&gt;Three new variables (&lt;CODE&gt;min_apple&lt;/CODE&gt;, &lt;CODE&gt;min_carrots&lt;/CODE&gt;, and &lt;CODE&gt;min_oranges&lt;/CODE&gt;) are calculated using the &lt;CODE&gt;min&lt;/CODE&gt; function and the &lt;CODE&gt;of&lt;/CODE&gt; operator.&lt;/LI&gt;
&lt;LI&gt;For &lt;CODE&gt;min_apple&lt;/CODE&gt;, it calculates the minimum value of all variables starting with 'Apples'.&lt;/LI&gt;
&lt;LI&gt;For &lt;CODE&gt;min_carrots&lt;/CODE&gt;, it calculates the minimum value of variables from &lt;CODE&gt;Carrots1&lt;/CODE&gt; to &lt;CODE&gt;Carrots20&lt;/CODE&gt;.&lt;/LI&gt;
&lt;LI&gt;For &lt;CODE&gt;min_oranges&lt;/CODE&gt;, it calculates the minimum value of the array &lt;CODE&gt;_oranges&lt;/CODE&gt; that includes &lt;CODE&gt;Oranges1&lt;/CODE&gt; through &lt;CODE&gt;Oranges20&lt;/CODE&gt;.&lt;/LI&gt;
&lt;/UL&gt;
&lt;/LI&gt;
&lt;LI&gt;
&lt;P&gt;&lt;STRONG&gt;Printing the New Dataset (&lt;CODE&gt;want&lt;/CODE&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;DIV class="bg-black rounded-md"&gt;
&lt;DIV class="flex items-center relative text-gray-200 bg-gray-800 gizmo:dark:bg-token-surface-primary px-4 py-2 text-xs font-sans justify-between rounded-t-md"&gt;&lt;SPAN&gt;sas&lt;/SPAN&gt;&lt;BUTTON class="flex ml-auto gizmo:ml-0 gap-1 items-center"&gt;Copy code&lt;/BUTTON&gt;&lt;/DIV&gt;
&lt;DIV class="p-4 overflow-y-auto"&gt;&lt;CODE class="!whitespace-pre hljs language-sas"&gt;proc print data=want;
  var min_:;
run;
&lt;/CODE&gt;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;UL&gt;
&lt;LI&gt;This part of the code prints the &lt;CODE&gt;want&lt;/CODE&gt; dataset, selecting the variables that start with 'min_'.&lt;/LI&gt;
&lt;/UL&gt;
&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;In summary, the code generates a dataset with 60 variables, sorts and selects specific variables based on their names, calculates the minimum values for selected variable groups, and prints the results.&lt;/P&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Nov 2023 03:59:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Group-variables-with-same-suffix-and-different-prefix/m-p/903834#M357122</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-11-20T03:59:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to Group variables with same suffix and different prefix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Group-variables-with-same-suffix-and-different-prefix/m-p/903905#M357135</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Awesome!&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Thank you so&lt;/SPAN&gt; much!&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;But for the&lt;/SPAN&gt; last part, where I create new variables, it gets tricky.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Could this work?&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;&lt;SPAN&gt;data have2;&lt;/SPAN&gt;&lt;BR /&gt;retain &amp;amp;varlist;&lt;BR /&gt;set have;&lt;BR /&gt;Array Apples_ {*} apples1-apples20;&lt;BR /&gt;Array Carrot_{*} Carrots1-carrots20;&lt;BR /&gt;Array oranges_{*} oranges1-oranges20;&lt;BR /&gt;do i=1 to dim(apples_);&lt;BR /&gt;Earlyfruit=min(of apples_[*]);&lt;BR /&gt;Afruit=Carrot_[min(of apples_[*])];&amp;nbsp; &amp;nbsp;/* I want the carrot that is chosen to match the suffix of Apple without changing the value of Carrot*/&lt;BR /&gt;Bfruit=Oranges_[min(of apples_[*])]; /* same here with Oranges*/&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Nov 2023 13:58:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Group-variables-with-same-suffix-and-different-prefix/m-p/903905#M357135</guid>
      <dc:creator>JMagenta</dc:creator>
      <dc:date>2023-11-20T13:58:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to Group variables with same suffix and different prefix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Group-variables-with-same-suffix-and-different-prefix/m-p/903911#M357137</link>
      <description>&lt;P&gt;This table structure may serve a purpose for reporting or specific analysis use cases, but it's not very practical for data storage.&lt;/P&gt;
&lt;P&gt;It would be better to store your data in a more flexible (normalised) manner, and then create the desired structure when needed.&lt;/P&gt;
&lt;P&gt;Also, I'm missing some kind id variable in your example...&lt;/P&gt;
&lt;P&gt;I'm suggesting something like&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;id date/period fruit fruit_no fruit_value&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;1 jan2023 orange 1 45&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then you can use formats or other means to group you values, and other kind of aggregation.&amp;nbsp; and then PROC TRANSPOSE to create your target layout.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Nov 2023 14:37:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Group-variables-with-same-suffix-and-different-prefix/m-p/903911#M357137</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2023-11-20T14:37:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to Group variables with same suffix and different prefix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Group-variables-with-same-suffix-and-different-prefix/m-p/903928#M357140</link>
      <description>&lt;P&gt;Whenever I see such structures, I see that there is data (fruit/vegetable types and sequence numbers) hidden in structure (variable names). Data belongs in variables, not in their names, so you should transpose your dataset and extract the relevant data from the _NAME_ variable you get from PROC TRANSPOSE.&lt;/P&gt;
&lt;P&gt;Then you can do it in SQL, which has the concept of sets (of observations), but not of arrays.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Nov 2023 15:26:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Group-variables-with-same-suffix-and-different-prefix/m-p/903928#M357140</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-11-20T15:26:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to Group variables with same suffix and different prefix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Group-variables-with-same-suffix-and-different-prefix/m-p/903933#M357143</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I get it, but to be clear,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This was the only way I could find to group the data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Each variable group:&lt;/P&gt;
&lt;P&gt;Apples1 Oranges 1 Carrots1 belongs to basket1.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Apples2 Oranges2 Carrots2 belongs to basket2 and so on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if Apples2 is the variable with the minimum value for that line for Basket1;&lt;/P&gt;
&lt;P&gt;Then basically what is Carrots2 and Oranges2:&lt;/P&gt;
&lt;P&gt;FruitA=Oranges2;&lt;/P&gt;
&lt;P&gt;FruitB=Carrots2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is what I want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sorry if it's not too clear.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Nov 2023 15:36:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Group-variables-with-same-suffix-and-different-prefix/m-p/903933#M357143</guid>
      <dc:creator>JMagenta</dc:creator>
      <dc:date>2023-11-20T15:36:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to Group variables with same suffix and different prefix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Group-variables-with-same-suffix-and-different-prefix/m-p/903934#M357144</link>
      <description>&lt;P&gt;I agree with the suggestion to transpose this data into a more vertical / normalized format to make it easier to work with.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/438491"&gt;@JMagenta&lt;/a&gt;&amp;nbsp;, could you post some example data?&amp;nbsp; Ideally, a DATA step that uses the CARDS statement with just a few rows and a few variables, maybe ID Apple1-Apple3 Carrot1-Carrot3 Orange1-Orange3?&amp;nbsp; Also please show the output data you would want for this example data.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Nov 2023 15:39:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Group-variables-with-same-suffix-and-different-prefix/m-p/903934#M357144</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-11-20T15:39:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to Group variables with same suffix and different prefix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Group-variables-with-same-suffix-and-different-prefix/m-p/903942#M357145</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have2;
retain &amp;amp;varlist;
set have;
Array Apples_ {*} apples1-apples20;
Array Carrot_{*} Carrots1-carrots20;
Array oranges_{*} oranges1-oranges20;

Earlyfruit=min(of apples_[*]);

index_earlyfruit = whichn(earlyfruit, of apples_(*));
afruit = carrot_(index_earlyfruit);
bfruit = oranges_(index_earlyfruit);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Use WHICHN to find the index of the minimum value. Note that if you have ties, the first one is returned.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/438491"&gt;@JMagenta&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;SPAN&gt;Awesome!&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Thank you so&lt;/SPAN&gt; much!&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;But for the&lt;/SPAN&gt; last part, where I create new variables, it gets tricky.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Could this work?&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;&lt;SPAN&gt;data have2;&lt;/SPAN&gt;&lt;BR /&gt;retain &amp;amp;varlist;&lt;BR /&gt;set have;&lt;BR /&gt;Array Apples_ {*} apples1-apples20;&lt;BR /&gt;Array Carrot_{*} Carrots1-carrots20;&lt;BR /&gt;Array oranges_{*} oranges1-oranges20;&lt;BR /&gt;do i=1 to dim(apples_);&lt;BR /&gt;Earlyfruit=min(of apples_[*]);&lt;BR /&gt;Afruit=Carrot_[min(of apples_[*])];&amp;nbsp; &amp;nbsp;/* I want the carrot that is chosen to match the suffix of Apple without changing the value of Carrot*/&lt;BR /&gt;Bfruit=Oranges_[min(of apples_[*])]; /* same here with Oranges*/&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Nov 2023 15:54:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Group-variables-with-same-suffix-and-different-prefix/m-p/903942#M357145</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-11-20T15:54:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to Group variables with same suffix and different prefix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Group-variables-with-same-suffix-and-different-prefix/m-p/903944#M357146</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data work.have;
  array Apples  {20} 8;
  array Carrots {20} 8;
  array Oranges {20} 8;
  do i=1 to dim(apples);
    apples[i]=i+1; /* I did "i+5" and so forth so that the numbers were unique values*/
    carrots[i]=i+5;
    oranges[i]=i+8;
  end;
  drop i;
run;


proc sql noprint;
  select name into :varlist separated by ' '
  from dictionary.columns
  where 
    libname='WORK' 
    and memname='HAVE'
    and (
             upcase(name) like 'APPLES%'
          or upcase(name) like 'CARROTS%'
          or upcase(name) like 'ORANGES%'
        )
  order by compress(name,,'kd'), name
  ;
quit;
data have2;
  retain &amp;amp;varlist;
	set have;
Array Apples_ {*} apples1-apples20;
Array Carrot_{*} Carrots1-carrots20;
Array oranges_{*} oranges1-oranges20;
 do i=1 to dim(apples_);
 	Earlyfruit=min(of apples_[*]);
	Afruit=Carrot_[min(of apples_[*])];
	Bfruit=Oranges_{min(of apples_[*])};

	end;

run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The result value for Easyfruit is correct and equals Apples1 which is 2.&lt;/P&gt;
&lt;P&gt;The result for Afruit &lt;STRONG&gt;should&lt;/STRONG&gt; equal Carrots1 which is 6 instead it equals 7 (likely the value of Apples6).&lt;/P&gt;
&lt;P&gt;The result for Bfruit &lt;STRONG&gt;should&lt;/STRONG&gt; equal Oranges 1 which is 9 instead it equals 10 (likely the value of Apples9).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Nov 2023 15:54:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Group-variables-with-same-suffix-and-different-prefix/m-p/903944#M357146</guid>
      <dc:creator>JMagenta</dc:creator>
      <dc:date>2023-11-20T15:54:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to Group variables with same suffix and different prefix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Group-variables-with-same-suffix-and-different-prefix/m-p/903945#M357147</link>
      <description>&lt;P&gt;Rid yourself of your Excel-thinking. In Excel, data is always stored in report-form, which is unsuited for computation (unless you have a spreadsheet).&lt;/P&gt;
&lt;P&gt;In real computing, data is stored in relational, sequential tables, with one variable for one fact.&lt;/P&gt;
&lt;P&gt;So let's transpose you dumb data to intelligent data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dumb;
input id $ apples1 apples2 apples3 carrots1 carrots2 carrots3 oranges1 oranges2 oranges3;
datalines;
1 219 285 185 463 192 316 219 352 196
;

proc transpose data=dumb out=intelligent1 (rename=(col1=value));
by id;
var apples: carrots: oranges:;
run;

data intelligent2;
set intelligent1;
i = anydigit(_name_);
fruit = substr(_name_,1,i-1);
seq = input(substr(_name_,i),best.);
drop i _name_;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can use SQL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select
  a.id,
  a.seq,
  a.fruit,
  a.value
from intelligent2 a
where a.seq in (
  select seq 
  from intelligent2 b 
  where b.id = a.id and b.fruit = "apples"
  group by b.id
  having b.value = max(b.value)
)
;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And create a wide report without hassle:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc report data=want;
column id seq value,fruit;
define id / group;
define seq / group;
define value / "" analysis;
define fruit / "" across;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;id	seq	apples	carrots	oranges
1	2	285	192	352
&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 Nov 2023 15:55:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Group-variables-with-same-suffix-and-different-prefix/m-p/903945#M357147</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-11-20T15:55:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to Group variables with same suffix and different prefix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Group-variables-with-same-suffix-and-different-prefix/m-p/903948#M357148</link>
      <description>&lt;P&gt;PS the long structure also allows easy analysis over different groupings, as you can use the values in fruit, or the sequence, as the main index. With the wide structure, this is hard to impossible.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See Maxim 19.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Nov 2023 15:58:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Group-variables-with-same-suffix-and-different-prefix/m-p/903948#M357148</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-11-20T15:58:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to Group variables with same suffix and different prefix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Group-variables-with-same-suffix-and-different-prefix/m-p/903953#M357149</link>
      <description>&lt;P&gt;To Everyone on this project!!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;THANK YOU!!!&lt;/P&gt;
&lt;P&gt;I am learning day by day, and you are each helping me do that!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;JMagenta&lt;/P&gt;</description>
      <pubDate>Mon, 20 Nov 2023 16:12:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Group-variables-with-same-suffix-and-different-prefix/m-p/903953#M357149</guid>
      <dc:creator>JMagenta</dc:creator>
      <dc:date>2023-11-20T16:12:05Z</dc:date>
    </item>
  </channel>
</rss>

