<?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 Replacing the last member of an array with a specific value in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Replacing-the-last-member-of-an-array-with-a-specific-value/m-p/364128#M64659</link>
    <description>&lt;P&gt;Hi everyone,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I a beginner with SAS and have a question. I am trying to create an array (CHF1-CHF-23) for each patient&amp;nbsp;in a dataset that has members as large as the longest years of follow-up (23 years). I can create the array using the following code:&lt;/P&gt;&lt;PRE&gt;proc sort data = merged2;
   by descending CHF_FU;
run;

data _NULL_;
   set merged2;
   if _N_=1 then call symput('cols',CHF_FU);
run;

%put Additional Columns: &amp;amp;cols;

proc sort data = merged2;   
   by ID;
run;
data merged2; set merged2;

   array CHF[&amp;amp;cols];
   
   i = 1;
   do until (i &amp;gt; CHF_FU);
      CHF[i] = 0;
      i + 1;
   end;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;However, now for select patients (UNCHF=1) I need to replace the last member of that array with a 1 instead of a 0. I don't know how to address the last member of the array for that subsample and replace it with a specific value.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
    <pubDate>Sun, 04 Jun 2017 15:53:52 GMT</pubDate>
    <dc:creator>Sina</dc:creator>
    <dc:date>2017-06-04T15:53:52Z</dc:date>
    <item>
      <title>Replacing the last member of an array with a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Replacing-the-last-member-of-an-array-with-a-specific-value/m-p/364128#M64659</link>
      <description>&lt;P&gt;Hi everyone,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I a beginner with SAS and have a question. I am trying to create an array (CHF1-CHF-23) for each patient&amp;nbsp;in a dataset that has members as large as the longest years of follow-up (23 years). I can create the array using the following code:&lt;/P&gt;&lt;PRE&gt;proc sort data = merged2;
   by descending CHF_FU;
run;

data _NULL_;
   set merged2;
   if _N_=1 then call symput('cols',CHF_FU);
run;

%put Additional Columns: &amp;amp;cols;

proc sort data = merged2;   
   by ID;
run;
data merged2; set merged2;

   array CHF[&amp;amp;cols];
   
   i = 1;
   do until (i &amp;gt; CHF_FU);
      CHF[i] = 0;
      i + 1;
   end;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;However, now for select patients (UNCHF=1) I need to replace the last member of that array with a 1 instead of a 0. I don't know how to address the last member of the array for that subsample and replace it with a specific value.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Sun, 04 Jun 2017 15:53:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Replacing-the-last-member-of-an-array-with-a-specific-value/m-p/364128#M64659</guid>
      <dc:creator>Sina</dc:creator>
      <dc:date>2017-06-04T15:53:52Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing the last member of an array with a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Replacing-the-last-member-of-an-array-with-a-specific-value/m-p/364134#M64660</link>
      <description>&lt;P&gt;Can you provide examples of the data you have and the data you want (both in the form of data steps)? That would make it a lot easier to answer your question.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 04 Jun 2017 16:26:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Replacing-the-last-member-of-an-array-with-a-specific-value/m-p/364134#M64660</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-06-04T16:26:44Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing the last member of an array with a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Replacing-the-last-member-of-an-array-with-a-specific-value/m-p/364135#M64661</link>
      <description>&lt;P&gt;First let's simplify the top of your program. If you want to put the maximum value of CHF_FU into a macro variable it is much easier to just use PROC SQL.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint ;
  select max(chf_fu) into :cols trimmed
  from merged2
  ;
quit;
%put Additional Columns: &amp;amp;cols;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now in your main program if you want to iterate over a series of integers the DO loop can do that directly. No need to manual set/increment your own counter.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new_merged2;
  set merged2;
  array CHF(&amp;amp;cols);
  do i=1 to CHF_FU-1 ;  
     CHF(i) = 0;
  end;
  CHF(CHF_FU)=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 04 Jun 2017 16:27:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Replacing-the-last-member-of-an-array-with-a-specific-value/m-p/364135#M64661</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-06-04T16:27:22Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing the last member of an array with a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Replacing-the-last-member-of-an-array-with-a-specific-value/m-p/364166#M64663</link>
      <description>&lt;P&gt;Thank you for your answer. I tried the following code since I don't want every patient to have a 1 at the end. Only the subset that has the event of interest:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data merged2; set merged2;

   array CHF[&amp;amp;cols];
   
   i = 1;
   do until (i &amp;gt; CHF_FU);
      CHF[i] = 0;
      i + 1;
   end;
   if UNCHF=1 then do j= CHF_FU - 1;
   CHF[j]=1;
   end;
   drop i j;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;However, this time I get the error message 'array subscript out of range'. Do you know where my mistake is? Thanks&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 04 Jun 2017 22:01:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Replacing-the-last-member-of-an-array-with-a-specific-value/m-p/364166#M64663</guid>
      <dc:creator>Sina</dc:creator>
      <dc:date>2017-06-04T22:01:15Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing the last member of an array with a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Replacing-the-last-member-of-an-array-with-a-specific-value/m-p/364169#M64664</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/122053"&gt;@Sina&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Thank you for your answer. I tried the following code since I don't want every patient to have a 1 at the end. Only the subset that has the event of interest:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data merged2; set merged2;

   array CHF[&amp;amp;cols];
   
   i = 1;
   do until (i &amp;gt; CHF_FU);
      CHF[i] = 0;
      i + 1;
   end;
   if UNCHF=1 then do j= CHF_FU - 1;
   CHF[j]=1;
   end;
   drop i j;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, this time I get the error message 'array subscript out of range'. Do you know where my mistake is? Thanks&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The error will actually tell you where I believe.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It helps to post your log in these cases.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suspect this line causes the issue, but without data/log it's just a guess.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;  do until &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;i &lt;SPAN class="token operator"&gt;&amp;gt;&lt;/SPAN&gt; CHF_FU&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This line likely generates the error:&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;CHF&lt;SPAN class="token punctuation"&gt;[&lt;/SPAN&gt;j&lt;SPAN class="token punctuation"&gt;]&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suspect there's an easier way to do this and if you posted sample data it would help.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 04 Jun 2017 22:20:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Replacing-the-last-member-of-an-array-with-a-specific-value/m-p/364169#M64664</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-06-04T22:20:45Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing the last member of an array with a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Replacing-the-last-member-of-an-array-with-a-specific-value/m-p/364170#M64665</link>
      <description>&lt;P&gt;This is the sample data:&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID &amp;nbsp; &amp;nbsp; &amp;nbsp;CHF &amp;nbsp; &amp;nbsp; CHF_FU&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 23&amp;nbsp;&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 14&lt;/P&gt;&lt;P&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 8&lt;/P&gt;&lt;P&gt;4 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5 &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID is patient ID. CHF is whether the patient has the disease, and CHF_FU indicates years of follow-up in the study. I want to create an array of 23 (Max of the years of follow-up) that has 23, 8, and 5 zeros for patients 1, 3, and 4, respectively. For the second patient, I want the array to have 13 zeros and in the fourteenth column a 1:&lt;/P&gt;&lt;P&gt;ID CHF1 CHF2 &lt;SPAN&gt;CHF3 CHF4 CHF5&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN&gt;CHF6 CHF7 CHF8 CHF9 CHF10 CHF11 CHF12 CHF13 CHF14 ... CHF22 CHF23&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1 &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2 &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; . &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 04 Jun 2017 22:29:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Replacing-the-last-member-of-an-array-with-a-specific-value/m-p/364170#M64665</guid>
      <dc:creator>Sina</dc:creator>
      <dc:date>2017-06-04T22:29:57Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing the last member of an array with a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Replacing-the-last-member-of-an-array-with-a-specific-value/m-p/364173#M64666</link>
      <description>&lt;P&gt;I suggest you start with that next time.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The solution is as proposed by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;initially, with a second IF to assign 1 if the CHF=1.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input ID CHF CHF_FU;
	cards;
1        0               23 
2        1               14
3        0               8
4        0               5 
;
run;

data want;
	set have;
	array _chf_(*) chf1-chf23;

	do i=1 to chf_fu-1;
		_chf_(i)=0;
	end;

	if chf=1 then
		_chf_(chf_fu)=1;
	else
		_chf_(chf_fu)=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 04 Jun 2017 22:43:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Replacing-the-last-member-of-an-array-with-a-specific-value/m-p/364173#M64666</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-06-04T22:43:48Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing the last member of an array with a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Replacing-the-last-member-of-an-array-with-a-specific-value/m-p/364175#M64667</link>
      <description>&lt;P&gt;So first let's convert your sample data into a dataset. Let's use UNCHF for the second variable to match what you used before and avoid conflict with the array name that you used before.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
 input ID UNCHF CHF_FU;
cards;
1 0 23 
2 1 14
3 0  8
4 0  5
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then let's find the maximum values of CHF_FU. Here is another method.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  retain max 0;
  if eof then call symputx('cols',max);
  set have end=eof;
  max=max(max,chf_fu);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you use a normal iterative DO loop the code will be easier to write and easier to understand.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have ;
  array CHF (&amp;amp;cols);
  do i=1 to chf_fu ;
    chf(i)=0;
  end;
  if unchf then chf(chf_fu)=1;
  drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here is the result:&lt;/P&gt;
&lt;PRE&gt;6249  data _null_;
6250    set want ;
6251    put (id unchf chf_fu) (3.) (chf1-chf23) (2.);
6252  run;

  1  0 23 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  2  1 14 0 0 0 0 0 0 0 0 0 0 0 0 0 1 . . . . . . . . .
  3  0  8 0 0 0 0 0 0 0 0 . . . . . . . . . . . . . . .
  4  0  5 0 0 0 0 0 . . . . . . . . . . . . . . . . . .
NOTE: There were 4 observations read from the data set WORK.WANT&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;The only way that it could give you index out of range errors would be if the value of CHF_FU is less than 1 or is not an integer. So perhaps you could test for that in the code?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have ;
  array CHF (&amp;amp;cols);
  if chf_fu &amp;lt; 1 or chf_fu ne int(chf_fu) then put 'ERROR: Invalid value. ' chf_fu=;
  else do;
    do i=1 to chf_fu ;
      chf(i)=0;
    end;
    if unchf then chf(chf_fu)=1;
  end;
  drop i;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 04 Jun 2017 22:55:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Replacing-the-last-member-of-an-array-with-a-specific-value/m-p/364175#M64667</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-06-04T22:55:05Z</dc:date>
    </item>
  </channel>
</rss>

