<?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: Concatenation Subsequent Column Values (Lag function?) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Concatenation-Subsequent-Column-Values-Lag-function/m-p/840002#M332132</link>
    <description>&lt;P&gt;This is FANTASTIC!&amp;nbsp; I didn't think it could be that simple, yet complex in concept. Thank you so much!!!&lt;/P&gt;</description>
    <pubDate>Fri, 21 Oct 2022 18:54:01 GMT</pubDate>
    <dc:creator>bendsteel6</dc:creator>
    <dc:date>2022-10-21T18:54:01Z</dc:date>
    <item>
      <title>Concatenation Subsequent Column Values (Lag function?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenation-Subsequent-Column-Values-Lag-function/m-p/839957#M332103</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I'm trying to combine values from a character column in a structured way.&amp;nbsp; To explain it, see the picture below:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="bendsteel6_2-1666366222941.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/76444iCB9785EF7434A878/image-size/medium?v=v2&amp;amp;px=400" role="button" title="bendsteel6_2-1666366222941.png" alt="bendsteel6_2-1666366222941.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I would like to create rows where the LC variable is concatenated with each of its subsequent values for the header.&amp;nbsp; In other words, in this case produce records with LC_combos of&amp;nbsp; A_B, A_C, A_D, A_E, then B_C, B_D, B_E, then C_D, C_E, then finally D_E.&amp;nbsp; In this case above, there are 10 possible unique combinations of the 5 LCs.&amp;nbsp; However, in my datasets the number of LCs per header can vary...I could have 5, or 7 or even 10.&amp;nbsp; I would also like to see which Job the LC's came from.&amp;nbsp; In the example above in the first output record, the LC 'A' comes from Job 10 and LC 'B' comes from Job 20.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;I've been trying to use the LAG function in a do loop but values are coming up empty.&amp;nbsp; Does anyone know if there's a simple method of accomplishing the above, other than using the LAG function?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It doesn't make sense to include my code here because it's all messed and doesn't work.&amp;nbsp; I'm looking for suggestions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 21 Oct 2022 15:35:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenation-Subsequent-Column-Values-Lag-function/m-p/839957#M332103</guid>
      <dc:creator>bendsteel6</dc:creator>
      <dc:date>2022-10-21T15:35:46Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenation Subsequent Column Values (Lag function?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenation-Subsequent-Column-Values-Lag-function/m-p/839961#M332107</link>
      <description>&lt;P&gt;Looks like a simple self join.&amp;nbsp; Probably easier in PROC SQL.&lt;/P&gt;
&lt;P&gt;If you want help writing the code then post the data (not a photograph of the data).&amp;nbsp; Best is to post it as a data step (one that works!).&lt;/P&gt;</description>
      <pubDate>Fri, 21 Oct 2022 15:47:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenation-Subsequent-Column-Values-Lag-function/m-p/839961#M332107</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-10-21T15:47:32Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenation Subsequent Column Values (Lag function?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenation-Subsequent-Column-Values-Lag-function/m-p/839986#M332120</link>
      <description>&lt;P&gt;I would love help in writing this.&amp;nbsp; Was trying to stay way from SQL but if that's the best way, then so be it.&amp;nbsp; Here's the code for the input data.&amp;nbsp; I'm still trying to work on this but am getting nowhere &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&amp;nbsp; .&amp;nbsp; I'll continue to try.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data Have;&lt;BR /&gt;format Header Job LC $10. Line Tot_Lines 4.;&lt;BR /&gt;infile datalines delimiter="|";&lt;BR /&gt;input&lt;BR /&gt;Header&lt;BR /&gt;Job&lt;BR /&gt;LC&lt;BR /&gt;Line&lt;BR /&gt;Tot_Lines&lt;BR /&gt;;&lt;BR /&gt;datalines;&lt;BR /&gt;x|10|A|1|5&lt;BR /&gt;x|20|B|2|5&lt;BR /&gt;x|30|C|3|5&lt;BR /&gt;x|30|D|4|5&lt;BR /&gt;x|50|E|5|5&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Oct 2022 17:34:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenation-Subsequent-Column-Values-Lag-function/m-p/839986#M332120</guid>
      <dc:creator>bendsteel6</dc:creator>
      <dc:date>2022-10-21T17:34:18Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenation Subsequent Column Values (Lag function?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenation-Subsequent-Column-Values-Lag-function/m-p/839988#M332122</link>
      <description>&lt;P&gt;Self + Cross join&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select a.header, a.lc, catx("_", a.lc, b.lc) as lc_combos, a.job as from _lc_job1, b.job as from _lc_job2
from have as a
cross join have as b
on a.header=b.header  /*join only when header is the same*/
and a.job ne b.job /*do not join with itself*/
 and a.job&amp;lt;b.job /*prevent A_B and B_A*/
order by 1, 2, 3;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Untested as no data was provided.&lt;/P&gt;</description>
      <pubDate>Fri, 21 Oct 2022 17:37:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenation-Subsequent-Column-Values-Lag-function/m-p/839988#M332122</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-10-21T17:37:12Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenation Subsequent Column Values (Lag function?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenation-Subsequent-Column-Values-Lag-function/m-p/839991#M332125</link>
      <description>&lt;P&gt;Yes. Simple self join.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Have;
  length Header Job LC $10 Line Tot_Lines 8;
  infile datalines dsd dlm="|" truncover;
  input Header -- Tot_lines;
datalines;
x|10|A|1|5
x|20|B|2|5
x|30|C|3|5
x|30|D|4|5
x|50|E|5|5
;

proc sql ;
create table want as
  select a.header
       , a.lc
       , catx('_',a.lc,b.lc) as lc_combo
       , a.job as job1
       , b.job as job2
  from have a 
  inner join have b
  on a.header = b.header
   and a.lc &amp;lt; b.lc
  order by 1,2,3
;
quit;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Obs    Header    LC    lc_combo    job1    job2

  1      x       A       A_B        10      20
  2      x       A       A_C        10      30
  3      x       A       A_D        10      30
  4      x       A       A_E        10      50
  5      x       B       B_C        20      30
  6      x       B       B_D        20      30
  7      x       B       B_E        20      50
  8      x       C       C_D        30      30
  9      x       C       C_E        30      50
 10      x       D       D_E        30      50

&lt;/PRE&gt;
&lt;P&gt;PS&amp;nbsp; The FORMAT statement is for attaching formats to the variable. FORMATS are instructions for how to DISPLAY the variables.&amp;nbsp; &amp;nbsp;If you want to DEFINE the variables use a LENGTH statement.&amp;nbsp; Notice also that most variables do not need to have special formats attached them.&amp;nbsp; SAS knows how to display character variables and numbers already.&lt;/P&gt;</description>
      <pubDate>Fri, 21 Oct 2022 17:49:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenation-Subsequent-Column-Values-Lag-function/m-p/839991#M332125</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-10-21T17:49:20Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenation Subsequent Column Values (Lag function?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenation-Subsequent-Column-Values-Lag-function/m-p/840002#M332132</link>
      <description>&lt;P&gt;This is FANTASTIC!&amp;nbsp; I didn't think it could be that simple, yet complex in concept. Thank you so much!!!&lt;/P&gt;</description>
      <pubDate>Fri, 21 Oct 2022 18:54:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenation-Subsequent-Column-Values-Lag-function/m-p/840002#M332132</guid>
      <dc:creator>bendsteel6</dc:creator>
      <dc:date>2022-10-21T18:54:01Z</dc:date>
    </item>
  </channel>
</rss>

