<?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: Output Value from a List of Variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Output-Value-from-a-List-of-Variables/m-p/508091#M136407</link>
    <description>Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt; for the explanation.</description>
    <pubDate>Sun, 28 Oct 2018 09:05:47 GMT</pubDate>
    <dc:creator>Jagadishkatam</dc:creator>
    <dc:date>2018-10-28T09:05:47Z</dc:date>
    <item>
      <title>Output Value from a List of Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-Value-from-a-List-of-Variables/m-p/507918#M136358</link>
      <description>&lt;P&gt;I&amp;nbsp; have a data set where individuals can come in at different times, stay in the data for a few years and then leave. I want to be able to flag when is their last year of data. So my data looks like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 384pt;" border="0" width="512" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 14.4pt;"&gt;
&lt;TD width="64" height="19" style="height: 14.4pt; width: 48pt;"&gt;Obs&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;in_2010&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;in_2011&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;in_2012&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;in_2013&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;in_2014&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;in_2015&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;in_2016&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.4pt;"&gt;
&lt;TD height="19" align="right" style="height: 14.4pt;"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.4pt;"&gt;
&lt;TD height="19" align="right" style="height: 14.4pt;"&gt;2&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.4pt;"&gt;
&lt;TD height="19" align="right" style="height: 14.4pt;"&gt;3&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.4pt;"&gt;
&lt;TD height="19" align="right" style="height: 14.4pt;"&gt;4&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.4pt;"&gt;
&lt;TD height="19" align="right" style="height: 14.4pt;"&gt;5&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.4pt;"&gt;
&lt;TD height="19" align="right" style="height: 14.4pt;"&gt;6&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.4pt;"&gt;
&lt;TD height="19" align="right" style="height: 14.4pt;"&gt;7&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.4pt;"&gt;
&lt;TD height="19" align="right" style="height: 14.4pt;"&gt;8&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I want to be able to create is a new variable that lists the last year of their enrollment in the data. There will be individuals who are in the data for the whole time period, or just arrive in 2016, and those are still considered active. But the ones who go from a 1 to 0, I want to capture. Since there are many possible combinations I don't want to hard code all the possibilities. Is there is a simpler way to do this? Thanks!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Oct 2018 23:36:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-Value-from-a-List-of-Variables/m-p/507918#M136358</guid>
      <dc:creator>endofline</dc:creator>
      <dc:date>2018-10-26T23:36:34Z</dc:date>
    </item>
    <item>
      <title>Re: Output Value from a List of Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-Value-from-a-List-of-Variables/m-p/507924#M136360</link>
      <description>&lt;P&gt;Define the variables in an array and loop backwards:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set have;
      array iny  in_: ;
      do i = dim(iny) to 1 by -1;
           if  iny(i) = 1 then do;
     
              year = substr(vname(iny(i)) , 3,4);
              leave;
          end;
     end;
run;
            &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 27 Oct 2018 02:09:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-Value-from-a-List-of-Variables/m-p/507924#M136360</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2018-10-27T02:09:43Z</dc:date>
    </item>
    <item>
      <title>Re: Output Value from a List of Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-Value-from-a-List-of-Variables/m-p/507931#M136361</link>
      <description>&lt;P&gt;My humble request is to post the desired output sample for the input for some dumb folks like me to figure out as well. Thank you!&lt;/P&gt;</description>
      <pubDate>Sat, 27 Oct 2018 02:28:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-Value-from-a-List-of-Variables/m-p/507931#M136361</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-10-27T02:28:28Z</dc:date>
    </item>
    <item>
      <title>Re: Output Value from a List of Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-Value-from-a-List-of-Variables/m-p/507968#M136380</link>
      <description>&lt;P&gt;I am not sure of the expected output but I assumed that this could be it. So please check the output and&amp;nbsp; if it the one expected then you may try this code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/24429i38B0B895C0F87BFA/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
array vars(*) in_2010	in_2011	in_2012	in_2013	in_2014	in_2015	in_2016;
do i = 1 to dim(vars);
if vars(i)=1 then year=compress(vname(vars(i)),,'kd');
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 27 Oct 2018 10:06:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-Value-from-a-List-of-Variables/m-p/507968#M136380</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2018-10-27T10:06:30Z</dc:date>
    </item>
    <item>
      <title>Re: Output Value from a List of Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-Value-from-a-List-of-Variables/m-p/508018#M136389</link>
      <description>&lt;P&gt;Good afternoon&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;, I hope you are having a great weekend.&lt;/P&gt;
&lt;P&gt;Just a nit assuming your understanding of what OP wants is desired&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have corrected your code as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
 set have;
 length year $4;
      array iny  in_: ;
      do i = dim(iny) to 1 by -1;
           if  iny(i) = 1 then do;
              year = substr(vname(iny(i)) , 4);

/*			 year = scan(vname(iny(i)) , -1,'_'); you could use scan too*/
              leave;
          end;
     end;
	 drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Correction :&lt;/P&gt;
&lt;P&gt;1. Starting position to extract from string. Since the values end with number for the year, stop value is not needed&lt;/P&gt;
&lt;P&gt;2. VNAME function extract defaults to a vlength of 200 bytes alike scan at compile time and this makes SAS pad with blanks to fill in for those unwanted bytes. So to counter this, a best practice would is to have length statement that assigns vlength at compile time&lt;/P&gt;
&lt;P&gt;3. Scan is another option if you wish. Well hardly makes&amp;nbsp; a difference, nevertheless i have included that option in the comment&lt;/P&gt;
&lt;P&gt;4. Perhaps there is a possibility of year being used for computation of intervals like differences etc. Well if OP chooses to do so, the code can be tweaked to&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
 set have;
/* length year $4;*/
      array iny  in_: ;
      do i = dim(iny) to 1 by -1;
           if  iny(i) = 1 then do;
              year =input( substr(vname(iny(i)) , 4),8.);

/*			 year =input( scan(vname(iny(i)) , -1,'_'),8.);*/
              leave;
          end;
     end;
	 drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;5. Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12151"&gt;@Jagadishkatam&lt;/a&gt;&amp;nbsp;In my humble opinion, your&amp;nbsp;forward loop checking&amp;nbsp; one by one for all the elements of the array and resetting everytime logic, makes your&amp;nbsp;year=compress(vname(vars(i)),,'kd'); execute everytime when&amp;nbsp;vars(i)=1 is true and that makes it not as efficient as the reverse loop and leave expended by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best Regards!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS In essence, we do not even need a loop aka linear approach, and whichn/vname alone would suffice. But in my test with 80 to 100 million records , linear does just as well or sometimes eclipses whichn although whichn code is much shorter.&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;
&lt;P&gt;&amp;nbsp;&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>Sat, 27 Oct 2018 20:41:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-Value-from-a-List-of-Variables/m-p/508018#M136389</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-10-27T20:41:12Z</dc:date>
    </item>
    <item>
      <title>Re: Output Value from a List of Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-Value-from-a-List-of-Variables/m-p/508091#M136407</link>
      <description>Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt; for the explanation.</description>
      <pubDate>Sun, 28 Oct 2018 09:05:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-Value-from-a-List-of-Variables/m-p/508091#M136407</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2018-10-28T09:05:47Z</dc:date>
    </item>
    <item>
      <title>Re: Output Value from a List of Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-Value-from-a-List-of-Variables/m-p/508892#M136714</link>
      <description>&lt;P&gt;Thanks, that output is very close to what I want.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 19:10:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-Value-from-a-List-of-Variables/m-p/508892#M136714</guid>
      <dc:creator>endofline</dc:creator>
      <dc:date>2018-10-30T19:10:32Z</dc:date>
    </item>
  </channel>
</rss>

