<?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: Combine the variable values vertically in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Combine-the-variable-values-vertically/m-p/988177#M43877</link>
    <description>Thank you very much for your help. Unfortunately the code that refers to the array does not work. But, anyway, how it become if the value of the variable is repeated identical?</description>
    <pubDate>Sat, 16 May 2026 16:29:52 GMT</pubDate>
    <dc:creator>NewUsrStat</dc:creator>
    <dc:date>2026-05-16T16:29:52Z</dc:date>
    <item>
      <title>Combine the variable values vertically</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Combine-the-variable-values-vertically/m-p/988172#M43872</link>
      <description>&lt;P&gt;Hi guys,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;suppose to have the following dataset:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$200. Reason1 :$200.Reason2 :$200.;
cards;
0001 Considered     Excluded
0001 Violated       Not_excluded
0001 Excluded       Not_Verified
0001 Excluded       Not_Verified   
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is there a way to get the following?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB1;
  input ID :$200. Reason1 :$200.Reason2 :$200.;
cards;
0001 Considered,Violated,Excluded     Excluded,Not_excluded,Not_Verified
0001 Considered,Violated,Excluded     Excluded,Not_excluded,Not_Verified
0001 Considered,Violated,Excluded     Excluded,Not_excluded,Not_Verified   
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In other words I would like to combine into one row and by each variable "Reason*" all the values the variable(s) takes.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that, after the combination, I would like to repeat the combination the number of times the variable Reason* takes a value ID by ID.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the real dataset I have Reason1 ,&amp;nbsp;Reason12,&amp;nbsp;Reason3, ...,&amp;nbsp;Reason10.&lt;/P&gt;
&lt;P&gt;Can anyone help me please?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 16 May 2026 16:32:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Combine-the-variable-values-vertically/m-p/988172#M43872</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2026-05-16T16:32:22Z</dc:date>
    </item>
    <item>
      <title>Re: Combine the variable values vertically</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Combine-the-variable-values-vertically/m-p/988173#M43873</link>
      <description>&lt;P&gt;In my opinion, this is not a good idea, and it makes future programming difficult. See &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Maxims-of-Maximally-Efficient-SAS-Programmers/ta-p/352068" target="_self"&gt;Maxim 19&lt;/A&gt;. Also, why do you need an output of three identical rows?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe a different re-arrangement of the data would work better. Please tell us what analyses/plots/tables you would create once you have the data re-arranged as you want?&lt;/P&gt;</description>
      <pubDate>Sat, 16 May 2026 15:05:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Combine-the-variable-values-vertically/m-p/988173#M43873</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2026-05-16T15:05:28Z</dc:date>
    </item>
    <item>
      <title>Re: Combine the variable values vertically</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Combine-the-variable-values-vertically/m-p/988174#M43874</link>
      <description>Thank you very much for your help. The reason why I have to replicate rows is because the dataset contains other variables with different values and for the subsequent analysis I have to apply different types of filters.</description>
      <pubDate>Sat, 16 May 2026 15:09:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Combine-the-variable-values-vertically/m-p/988174#M43874</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2026-05-16T15:09:00Z</dc:date>
    </item>
    <item>
      <title>Re: Combine the variable values vertically</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Combine-the-variable-values-vertically/m-p/988175#M43875</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/134532"&gt;@NewUsrStat&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thank you very much for your help. The reason why I have to replicate rows is because the dataset contains other variables with different values and for the subsequent analysis I have to apply different types of filters.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Unfortunately, this does not answer my question about what analyses/plots/tables are you trying to create.&lt;/P&gt;</description>
      <pubDate>Sat, 16 May 2026 15:10:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Combine-the-variable-values-vertically/m-p/988175#M43875</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2026-05-16T15:10:10Z</dc:date>
    </item>
    <item>
      <title>Re: Combine the variable values vertically</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Combine-the-variable-values-vertically/m-p/988176#M43876</link>
      <description>&lt;P&gt;You will need to make NEW variable(s).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have end=eof;
  length newreason $200 ;
  retain newreason;
  newreason = catx(',',newreason,reason);
  if eof; 
  drop reason;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the data is grouped by something, perhaps your ID variable, then this can be simplified by by&amp;nbsp; placing the SET inside a DO loop and using a BY statement.&amp;nbsp; This will eliminate the need for the RETAIN statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do until (last.id);
     set have;
     by id;
     length newreason $200;
     newreason=catx(',',newreason,reason);
  end;
  drop reason;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To output multiple copies you could change the DO statement to count the number of observations and then add a second DO loop around an output statement.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do _n_=1 by 1 until (last.id);
     set have;
     by id;
     length newreason $200;
     newreason=catx(',',newreason,reason);
  end;
  do _n_=1 to _n_;
    output;
  end;
  drop reason;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But if there are other variables besides ID and REASON you want to keep then instead just add another DO loop with and additional SET statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do until (last.id);
     set have;
     by id;
     length newreason $200;
     newreason=catx(',',newreason,reason);
  end;
  do until (last.id);
     set have;
     by id;
     output;
  end;
  drop reason;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And to handle multiple variables just use arrays.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do until (last.id);
     set have;
     by id;
     array reason[10];
     array newreason[10] $200;
     do over reason;
       newreason=catx(',',newreason,reason);
     end;
  end;
  do until (last.id);
     set have;
     by id;
     output;
  end;
  drop reason1-reason10;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you don't like using the deprecated DO OVER syntax then just add explicit indexes instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;     do _n_=1 to dim(reason);
       newreason[_n_]=catx(',',newreason[_n_],reason[_n_]);
     end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 16 May 2026 15:14:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Combine-the-variable-values-vertically/m-p/988176#M43876</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2026-05-16T15:14:06Z</dc:date>
    </item>
    <item>
      <title>Re: Combine the variable values vertically</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Combine-the-variable-values-vertically/m-p/988177#M43877</link>
      <description>Thank you very much for your help. Unfortunately the code that refers to the array does not work. But, anyway, how it become if the value of the variable is repeated identical?</description>
      <pubDate>Sat, 16 May 2026 16:29:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Combine-the-variable-values-vertically/m-p/988177#M43877</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2026-05-16T16:29:52Z</dc:date>
    </item>
    <item>
      <title>Re: Combine the variable values vertically</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Combine-the-variable-values-vertically/m-p/988178#M43878</link>
      <description>&lt;P&gt;If the values repeat then they will appear multiple times.&lt;/P&gt;
&lt;P&gt;If the goal is to get the distinct values then you would need to make the logic more complex so that the current reason is only added when it does not already appear.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do _n_=1 to dim(reason);
  if not findw(newreason[_n_],reason[_n_],',','t') then 
   newreason[_n_]=catx(',',newreason[_n_],reason[_n_])
  ;
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you don't care about the case then add the i modifier into addition to the t modifier in the FINDW() function call.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But with these extra complication it might be better to use multiple passes so the logic can be simpler and easier to follow.&amp;nbsp; So first build the list of unique values.&amp;nbsp; Then build the concatenations. And finally remerge it with the original data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Make TALL dataset with all of the reasons from all of the reason variables ;
proc transpose data=have out=step1;
   by id;
   var reason1-reason10;
run;
* Eliminate duplicates ;
proc sort data=step1 out=step2 nodupkey ;
   by id _name_ col1 ;
run;
* Concatenate ;
data step3;
   do until (last._name_);
      set step2;
      by id _name_;
      length list $200 ;
      list = catx(',',list,col1);
   end;
   drop col1;
run;
* Turn back into separate variables;
proc transpose data=step3 out=step4 ;
   by id;
   id _name_;
   var list ;
run;
* Combine with original data ;
data want;
   merge have(drop=reason1-reason10) step4;
   by id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 16 May 2026 17:47:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Combine-the-variable-values-vertically/m-p/988178#M43878</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2026-05-16T17:47:01Z</dc:date>
    </item>
    <item>
      <title>Re: Combine the variable values vertically</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Combine-the-variable-values-vertically/m-p/988182#M43879</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/134532"&gt;@NewUsrStat&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thank you very much for your help. Unfortunately the code that refers to the array does not work. But, anyway, how it become if the value of the variable is repeated identical?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;What does "array does not work"?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If code throws an error then provide the log with the code and all the messages. Copy the text from the log then on the forum open a text box in a message using the &amp;lt;/&amp;gt; icon above the message window and paste the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the result is not as expected then provide the data used in the form of a working data step and then provide more detail as to the expected result for the example data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am afraid that a generic "does not work" is very unhelpful in diagnosing problems.&lt;/P&gt;</description>
      <pubDate>Sat, 16 May 2026 19:46:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Combine-the-variable-values-vertically/m-p/988182#M43879</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2026-05-16T19:46:02Z</dc:date>
    </item>
  </channel>
</rss>

