<?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: Array with value of variable as start in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Array-with-value-of-variable-as-start/m-p/802278#M315823</link>
    <description>&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID	y_1201	y_1202	y_1203	y_1204	y_1205	y_1206	begin $;
cards;
1	100	100	100	200	200	200	y_1203
2	300	100	100	300	200	200	y_1201
;
run;
 
data want;
  set have;
  array y(i) y_:;
  array week[6];

  output = 0;
  do over y;
    if vname(y) = begin then output = 1;    
    if 0 &amp;lt; output &amp;lt;= dim(week) then 
      do;
        week[output] = y;
        output + 1;
      end;
  end;
  output;
keep id week:;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
    <pubDate>Tue, 15 Mar 2022 16:41:45 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2022-03-15T16:41:45Z</dc:date>
    <item>
      <title>Array with value of variable as start</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-with-value-of-variable-as-start/m-p/802254#M315814</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have weekly data, with each week of every year having a variable (first six week of 2012 shown in example, however my dataset covers multiple years).&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've made a variable "begin" which indicates when each individual was assigned to treatment.&amp;nbsp;&lt;/P&gt;&lt;P&gt;My goal is to create a dataset showing the first 50 weeks of treatment for each individual.&lt;/P&gt;&lt;P&gt;Week1 should equal y_1203 (=100) for ID=1, y_1201(=300) for ID=2 and so on.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've have tried multiple variations of arrays, however I cannot seem to get it right.&lt;/P&gt;&lt;P&gt;Is there any way to begin the array at the variable equal to the value of "begin" for each ID?&lt;/P&gt;&lt;P&gt;Example of data:&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;y_1201&lt;/TD&gt;&lt;TD&gt;y_1202&lt;/TD&gt;&lt;TD&gt;y_1203&lt;/TD&gt;&lt;TD&gt;y_1204&lt;/TD&gt;&lt;TD&gt;y_1205&lt;/TD&gt;&lt;TD&gt;y_1206&lt;/TD&gt;&lt;TD&gt;begin&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;TD&gt;y_1203&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;300&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;300&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;TD&gt;y_1201&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Goal example (only first 6 weeks shown here):&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;week1&lt;/TD&gt;&lt;TD&gt;week2&lt;/TD&gt;&lt;TD&gt;week3&lt;/TD&gt;&lt;TD&gt;week4&lt;/TD&gt;&lt;TD&gt;week5&lt;/TD&gt;&lt;TD&gt;week6&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;300&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;300&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Thanks for helping!&lt;/P&gt;</description>
      <pubDate>Tue, 15 Mar 2022 15:35:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-with-value-of-variable-as-start/m-p/802254#M315814</guid>
      <dc:creator>EmilieKL</dc:creator>
      <dc:date>2022-03-15T15:35:55Z</dc:date>
    </item>
    <item>
      <title>Re: Array with value of variable as start</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-with-value-of-variable-as-start/m-p/802268#M315822</link>
      <description>You can if you index your array not using 1,2, 3 and have a manual index set up instead. Then you can parse the number and start your array there. &lt;BR /&gt;&lt;BR /&gt;However, a better and more dynamic solution is to have your data in a long format rather than a wide format, then check when begin matches your ID column. From there start counting weeks. Then if you really want a wide format, you would transpose it. &lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 15 Mar 2022 16:16:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-with-value-of-variable-as-start/m-p/802268#M315822</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-03-15T16:16:25Z</dc:date>
    </item>
    <item>
      <title>Re: Array with value of variable as start</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-with-value-of-variable-as-start/m-p/802278#M315823</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID	y_1201	y_1202	y_1203	y_1204	y_1205	y_1206	begin $;
cards;
1	100	100	100	200	200	200	y_1203
2	300	100	100	300	200	200	y_1201
;
run;
 
data want;
  set have;
  array y(i) y_:;
  array week[6];

  output = 0;
  do over y;
    if vname(y) = begin then output = 1;    
    if 0 &amp;lt; output &amp;lt;= dim(week) then 
      do;
        week[output] = y;
        output + 1;
      end;
  end;
  output;
keep id week:;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 15 Mar 2022 16:41:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-with-value-of-variable-as-start/m-p/802278#M315823</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2022-03-15T16:41:45Z</dc:date>
    </item>
    <item>
      <title>Re: Array with value of variable as start</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-with-value-of-variable-as-start/m-p/802281#M315824</link>
      <description>&lt;P&gt;You could do:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID    y_1201  y_1202  y_1203  y_1204  y_1205  y_1206  begin $;
datalines;
1 100 100 100 200 200 200 y_1203
2 300 100 100 300 200 200 y_1201
;

data want;

array x week1-week50;

set have;
array y y_:;

w = 1;
start = 0;
do i = 1 to dim(y) until (w &amp;gt; dim(x));
    if vname(y{i}) = begin then start = 1;
    if start then do;
        x{w} = y{i};
        w = w + 1;
        end;
    end;
keep id week:;
run;
    
proc print data=want noobs; var id week1-week6; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PGStats_0-1647362928309.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/69479iDFA42D903A12EA97/image-size/medium?v=v2&amp;amp;px=400" role="button" title="PGStats_0-1647362928309.png" alt="PGStats_0-1647362928309.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Mar 2022 16:49:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-with-value-of-variable-as-start/m-p/802281#M315824</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2022-03-15T16:49:13Z</dc:date>
    </item>
    <item>
      <title>Re: Array with value of variable as start</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-with-value-of-variable-as-start/m-p/802608#M315975</link>
      <description>&lt;P&gt;Thank you very much, this works perfectly and gets the exact result I was looking for!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you reckon it would be possible to get the 6 week prior to the assignment of treatment as well?&amp;nbsp;&lt;/P&gt;&lt;P&gt;(e.g. variables week_6-week_1)&lt;/P&gt;</description>
      <pubDate>Thu, 17 Mar 2022 10:19:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-with-value-of-variable-as-start/m-p/802608#M315975</guid>
      <dc:creator>EmilieKL</dc:creator>
      <dc:date>2022-03-17T10:19:54Z</dc:date>
    </item>
    <item>
      <title>Re: Array with value of variable as start</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-with-value-of-variable-as-start/m-p/802826#M316093</link>
      <description>&lt;P&gt;Maybe like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID	begin $;
array y[*] y_1201-y_1252 (1:52);
cards;
1	y_1203
2	y_1212
3 y_1248
;
run;
 
data want;
  set have;
  array y[*] y_:;
  array week[1:12] week_6-week_1 week1-week6;

  output = 0; j = 13;
  do i = lbound(y) to hbound(y);
    if vname(y[i]) = begin then 
      do;
        output = 1;
        j = i - 6;
        leave; 
      end;
  end;

  if output then 
    do output = lbound(week) to hbound(week);
        if lbound(y) &amp;lt;= j &amp;lt;= hbound(y) then week[output] = y[j];
        j + 1;
      end;

  output;
keep id week:;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Fri, 18 Mar 2022 15:20:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-with-value-of-variable-as-start/m-p/802826#M316093</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2022-03-18T15:20:49Z</dc:date>
    </item>
  </channel>
</rss>

