<?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: Transposing with multiples values per ID and year. in Advanced Programming</title>
    <link>https://communities.sas.com/t5/Advanced-Programming/Transposing-with-multiples-values-per-ID-and-year/m-p/866024#M221</link>
    <description>&lt;P&gt;If you want to generate multiple observations per ID then you need some other variable that can distinguish the observations that go together.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data for_transpose ;
  set table;
  by id acc_date sequence ;
  row+1;
  if first.sequence then row=1;
run;

proc sort;
  by id row;
run;

proc transpose data = for_transpose out = Table_T1 name= VARIABLE  DELIM=_  prefix=X_; 
  by ID row;
  id ACC_DATE SEQUENCE; 
  var var_a var_b; 
run;

proc sort;
  by id variable row;
run;

proc print data=table_t1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1679620019668.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/81966iC2595456A3E7D883/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1679620019668.png" alt="Tom_0-1679620019668.png" /&gt;&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;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 24 Mar 2023 01:07:18 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-03-24T01:07:18Z</dc:date>
    <item>
      <title>Transposing with multiples values per ID and year.</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Transposing-with-multiples-values-per-ID-and-year/m-p/865338#M218</link>
      <description>&lt;P&gt;Hey there, spending way too much time trying to find a workaround to this issue but is time to ask for support.&lt;/P&gt;&lt;P&gt;I created a macro that transposed data from a wide format (variables names in columns, values in rows) to a long one (variable name in one column, values as columns per year and sequence). It works very well when values are unique, but failed for some datasets as there are multiple observations for same ID and YEAR_SEQUENCE. It does not really failed but got unwanted results. This is due to the LET function in PROC TRANSPOSE that allows the macro to continue (with warnings)&amp;nbsp;but the desired output is not what I need as only the last row per ID is outputted.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The main goal is to flag when there was a change in data values between the sequence numbers (e.g. 1 if there was a change between sequence 1 and sequence 2, or sequence 2 and sequence 3).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is a dummy example:&lt;/P&gt;&lt;P&gt;As you can see, for ID 2, Var_A has values STEP_A and STEP_B for same period (year and sequence), likewise VAR_B has two values for same period.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help will be pretty much appreciated.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;data table; 
input ID $ ACC_DATE SEQUENCE VAR_A $ VAR_B  ; 
datalines; 
1 202201 1 STEP_B 350 
1 202201 2 STEP_B 500 
1 202201 3 STEP_B 700 
2 202201 1 STEP_A 20 
2 202201 1 STEP_B 5 
2 202201 2 STEP_A 20 
2 202201 2 STEP_B 5 
2 202201 3 STEP_A 20 
2 202201 3 STEP_B 5 ; run; 

PROC SORT DATA= TABLE FORCE; BY ID ACC_DATE SEQUENCE  ; RUN; 

proc transpose data = work.Table out = work.Table_T1 name= VARIABLE  PREFIX=_ DELIM=WD let ; 
by ID; var var_a var_b; 
id ACC_DATE SEQUENCE; run;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt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D&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Tue, 21 Mar 2023 02:21:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Transposing-with-multiples-values-per-ID-and-year/m-p/865338#M218</guid>
      <dc:creator>Pechan_Uvas</dc:creator>
      <dc:date>2023-03-21T02:21:28Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing with multiples values per ID and year.</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Transposing-with-multiples-values-per-ID-and-year/m-p/866017#M219</link>
      <description>&lt;P&gt;Should the values of VAR_B in the last 4 rows of the initial data table match those in your "want" table?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so, then the only way I could think to do this (other than transposing with the DATA step) is to add an additional SEQ_CNT variable for proc transpose to use as a BY variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table; 
input ID $ ACC_DATE SEQUENCE VAR_A $ VAR_B  ; 
datalines; 
1 202201 1 STEP_B 350
1 202201 2 STEP_B 500
1 202201 3 STEP_B 700 
2 202201 1 STEP_A 20
2 202201 1 STEP_B 5
2 202201 2 STEP_A 40
2 202201 2 STEP_B 10
2 202201 3 STEP_A 80 
2 202201 3 STEP_B 10
; 
run; 

PROC SORT DATA= TABLE FORCE; BY ID ACC_DATE SEQUENCE; RUN;

* create SEQ_CNT variable;
data table;
   set table;
   by ID ACC_DATE SEQUENCE;
   if first.SEQUENCE then SEQ_CNT=0;
   SEQ_CNT+1;
run;

PROC SORT DATA= TABLE FORCE; BY ID SEQ_CNT; RUN;

proc transpose data=work.Table out=work.Table_T1 (drop=SEQ_CNT) name=VARIABLE  PREFIX=_ DELIM=_ let; 
   by ID SEQ_CNT; 
   var var_a var_b; 
   id ACC_DATE SEQUENCE; 
run;

proc sort data=Table_T1;
   by ID VARIABLE;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="want.JPG" style="width: 419px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/81964i82D6325481021165/image-size/large?v=v2&amp;amp;px=999" role="button" title="want.JPG" alt="want.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Mar 2023 23:56:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Transposing-with-multiples-values-per-ID-and-year/m-p/866017#M219</guid>
      <dc:creator>DerylHollick</dc:creator>
      <dc:date>2023-03-23T23:56:00Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing with multiples values per ID and year.</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Transposing-with-multiples-values-per-ID-and-year/m-p/866018#M220</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/441027"&gt;@Pechan_Uvas&lt;/a&gt; You should be aware that when you provide data as data step with Datalines or Cards that if you place the semicolon that ends the data block on a line with data that the line with the semicolon is not in the resulting dataset.&lt;/P&gt;
&lt;P&gt;So please make sure that your data steps look like:&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;data table; 
input ID $ ACC_DATE SEQUENCE VAR_A $ VAR_B  ; 
datalines; 
1 202201 1 STEP_B 350 
1 202201 2 STEP_B 500 
1 202201 3 STEP_B 700 
2 202201 1 STEP_A 20 
2 202201 1 STEP_B 5 
2 202201 2 STEP_A 20 
2 202201 2 STEP_B 5 
2 202201 3 STEP_A 20 
2 202201 3 STEP_B 5 
;  &lt;/LI-CODE&gt;
&lt;P&gt;Note that the Run is not needed.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Mar 2023 00:13:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Transposing-with-multiples-values-per-ID-and-year/m-p/866018#M220</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-03-24T00:13:01Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing with multiples values per ID and year.</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Transposing-with-multiples-values-per-ID-and-year/m-p/866024#M221</link>
      <description>&lt;P&gt;If you want to generate multiple observations per ID then you need some other variable that can distinguish the observations that go together.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data for_transpose ;
  set table;
  by id acc_date sequence ;
  row+1;
  if first.sequence then row=1;
run;

proc sort;
  by id row;
run;

proc transpose data = for_transpose out = Table_T1 name= VARIABLE  DELIM=_  prefix=X_; 
  by ID row;
  id ACC_DATE SEQUENCE; 
  var var_a var_b; 
run;

proc sort;
  by id variable row;
run;

proc print data=table_t1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1679620019668.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/81966iC2595456A3E7D883/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1679620019668.png" alt="Tom_0-1679620019668.png" /&gt;&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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Mar 2023 01:07:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Transposing-with-multiples-values-per-ID-and-year/m-p/866024#M221</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-03-24T01:07:18Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing with multiples values per ID and year.</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Transposing-with-multiples-values-per-ID-and-year/m-p/866072#M222</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/441027"&gt;@Pechan_Uvas&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The main goal is to flag when there was a change in data values between the sequence numbers (e.g. 1 if there was a change between sequence 1 and sequence 2, or sequence 2 and sequence 3).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;data table; 
input ID $ ACC_DATE SEQUENCE VAR_A $ VAR_B  ; 
datalines; 
1 202201 1 STEP_B 350 
1 202201 2 STEP_B 500 
1 202201 3 STEP_B 700 
2 202201 1 STEP_A 20 
2 202201 1 STEP_B 5 
2 202201 2 STEP_A 20 
2 202201 2 STEP_B 5 
2 202201 3 STEP_A 20 
2 202201 3 STEP_B 5 ; run; 

PROC SORT DATA= TABLE FORCE; BY ID ACC_DATE SEQUENCE  ; RUN; 

proc transpose data = work.Table out = work.Table_T1 name= VARIABLE  PREFIX=_ DELIM=WD let ; 
by ID; var var_a var_b; 
id ACC_DATE SEQUENCE; run;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;want&lt;/STRONG&gt;&lt;/P&gt;
&lt;TABLE border="1"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;ID&lt;/TD&gt;
&lt;TD&gt;Variable&lt;/TD&gt;
&lt;TD&gt;_202201_1&lt;/TD&gt;
&lt;TD&gt;_202201_2&lt;/TD&gt;
&lt;TD&gt;_202201_3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;VAR_A&lt;/TD&gt;
&lt;TD&gt;STEP_B&lt;/TD&gt;
&lt;TD&gt;STEP_B&lt;/TD&gt;
&lt;TD&gt;STEP_B&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;VAR_B&lt;/TD&gt;
&lt;TD&gt;350&lt;/TD&gt;
&lt;TD&gt;500&lt;/TD&gt;
&lt;TD&gt;700&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;VAR_A&lt;/TD&gt;
&lt;TD&gt;STEP_A&lt;/TD&gt;
&lt;TD&gt;STEP_A&lt;/TD&gt;
&lt;TD&gt;STEP_A&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;VAR_A&lt;/TD&gt;
&lt;TD&gt;STEP_B&lt;/TD&gt;
&lt;TD&gt;STEP_B&lt;/TD&gt;
&lt;TD&gt;STEP_B&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;VAR_B&lt;/TD&gt;
&lt;TD&gt;20&lt;/TD&gt;
&lt;TD&gt;40&lt;/TD&gt;
&lt;TD&gt;80&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;VAR_B&lt;/TD&gt;
&lt;TD&gt;5&lt;/TD&gt;
&lt;TD&gt;10&lt;/TD&gt;
&lt;TD&gt;10&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If your goal is to flag data values being different, transposing is not needed (and results in a horrible and difficult to use formatting of the table). You can use the LAG function in data step, without transposing, to obtain flags when data values are different.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Mar 2023 10:22:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Transposing-with-multiples-values-per-ID-and-year/m-p/866072#M222</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-03-24T10:22:12Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing with multiples values per ID and year.</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Transposing-with-multiples-values-per-ID-and-year/m-p/869794#M228</link>
      <description>&lt;P&gt;This solution also works very well. Never seen the use of row+1 in a BY statement. Thanks for proposing a different approach.&lt;/P&gt;</description>
      <pubDate>Fri, 14 Apr 2023 14:01:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Transposing-with-multiples-values-per-ID-and-year/m-p/869794#M228</guid>
      <dc:creator>Pechan_Uvas</dc:creator>
      <dc:date>2023-04-14T14:01:48Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing with multiples values per ID and year.</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Transposing-with-multiples-values-per-ID-and-year/m-p/869799#M229</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/441027"&gt;@Pechan_Uvas&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;This solution also works very well. Never seen the use of row+1 in a BY statement. Thanks for proposing a different approach.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ROW+1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is a SUM statement.&amp;nbsp; It has nothing directly to do with the BY statement.&amp;nbsp; It is the IF statement that is using the BY statement because it is referencing the FIRST. variable created by the BY statement.&lt;/P&gt;</description>
      <pubDate>Fri, 14 Apr 2023 14:15:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Transposing-with-multiples-values-per-ID-and-year/m-p/869799#M229</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-04-14T14:15:10Z</dc:date>
    </item>
  </channel>
</rss>

