<?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: How Do I Subtract Many Different Variables from Another in Least Amount of Code in SAS Studio</title>
    <link>https://communities.sas.com/t5/SAS-Studio/How-Do-I-Subtract-Many-Different-Variables-from-Another-in-Least/m-p/402417#M3633</link>
    <description>&lt;P&gt;The usual method for processing many variables in the same fashion is to use arrays:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;array in {119} in1-in119;&lt;/P&gt;
&lt;P&gt;array out {119} out1-out119;&lt;/P&gt;
&lt;P&gt;array diff {119} gym_visit1-gym_visit119;&lt;/P&gt;
&lt;P&gt;do _n_=1 to 119;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if out{_n_} &amp;gt; . then diff{_n_} = out{_n_} - in{_n_};&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alternatively, you could have macro language generate 119 statements for you.&amp;nbsp; In its simplest form:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro differ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; %local j;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; %do j=1 %to 119;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gym_visit&amp;amp;j = out&amp;amp;j - in&amp;amp;j;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;%mend differ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;%differ&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;More complex macro coding would let you control how many statements to generate (instead of hard-coding 119), or the names of the incoming and outgoing variables.&amp;nbsp; In this case, simplest seems appropriate.&lt;/P&gt;</description>
    <pubDate>Mon, 09 Oct 2017 16:09:45 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2017-10-09T16:09:45Z</dc:date>
    <item>
      <title>How Do I Subtract Many Different Variables from Another in Least Amount of Code</title>
      <link>https://communities.sas.com/t5/SAS-Studio/How-Do-I-Subtract-Many-Different-Variables-from-Another-in-Least/m-p/402403#M3632</link>
      <description>&lt;P&gt;I am working with a dataset of time spent at the gym in SAS Studio.&amp;nbsp; This includes an ID, time-in, and time-out.&amp;nbsp; I have 119 columns for time-in and 119 for&amp;nbsp;time-out.&amp;nbsp; I was able to input the data using the variable list in the screenshot attached.&amp;nbsp; Is there a similar way to perform gym_visit1 = out1 - in1 to get the amount of time spent at the gym per session without writing 119 different calculations?&amp;nbsp; Thank you.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2017 15:41:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/How-Do-I-Subtract-Many-Different-Variables-from-Another-in-Least/m-p/402403#M3632</guid>
      <dc:creator>RBRoma</dc:creator>
      <dc:date>2017-10-09T15:41:19Z</dc:date>
    </item>
    <item>
      <title>Re: How Do I Subtract Many Different Variables from Another in Least Amount of Code</title>
      <link>https://communities.sas.com/t5/SAS-Studio/How-Do-I-Subtract-Many-Different-Variables-from-Another-in-Least/m-p/402417#M3633</link>
      <description>&lt;P&gt;The usual method for processing many variables in the same fashion is to use arrays:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;array in {119} in1-in119;&lt;/P&gt;
&lt;P&gt;array out {119} out1-out119;&lt;/P&gt;
&lt;P&gt;array diff {119} gym_visit1-gym_visit119;&lt;/P&gt;
&lt;P&gt;do _n_=1 to 119;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if out{_n_} &amp;gt; . then diff{_n_} = out{_n_} - in{_n_};&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alternatively, you could have macro language generate 119 statements for you.&amp;nbsp; In its simplest form:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro differ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; %local j;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; %do j=1 %to 119;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gym_visit&amp;amp;j = out&amp;amp;j - in&amp;amp;j;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;%mend differ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;%differ&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;More complex macro coding would let you control how many statements to generate (instead of hard-coding 119), or the names of the incoming and outgoing variables.&amp;nbsp; In this case, simplest seems appropriate.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2017 16:09:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/How-Do-I-Subtract-Many-Different-Variables-from-Another-in-Least/m-p/402417#M3633</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-10-09T16:09:45Z</dc:date>
    </item>
    <item>
      <title>Re: How Do I Subtract Many Different Variables from Another in Least Amount of Code</title>
      <link>https://communities.sas.com/t5/SAS-Studio/How-Do-I-Subtract-Many-Different-Variables-from-Another-in-Least/m-p/402647#M3642</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/164022"&gt;@RBRoma&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Everything will become simple if you transpose your wide data structure to a long data structure.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using the code you've posted below amended code (not tested as no sample data available) should do the trick:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data newyear(keep=id t_in t_out) ;
  infile '/folders/myshortcuts/SAS/SAS HW #2/Data_Assignment2/NewYears.dat' dsd truncover ;
  input id (in1-in119 out1-out119) (:time8.) ;
  format in1-in119 out1-out119 time8.;
  array a_in {*} in:;
  array a_out {*} out:;
  do _i=1 to dim(a_in);
    t_in=a_in[_i];
    t_out_t=a_out[_i];
    if cmiss(t_in,t_out)&amp;gt;0 then output;
  end;
run;

data gymvisit;
  set newyear;
  duration_in_sec = t_in - t_out;
  format duration_in_sec time8.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Oct 2017 08:59:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/How-Do-I-Subtract-Many-Different-Variables-from-Another-in-Least/m-p/402647#M3642</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-10-10T08:59:39Z</dc:date>
    </item>
  </channel>
</rss>

