<?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 to substrate two variables by keywords in variable name? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-substrate-two-variables-by-keywords-in-variable-name/m-p/536424#M147417</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a solution where the code is generated by call execute's. It uses a loop to retrieve the names&lt;/P&gt;
&lt;P&gt;of the dataset's columns with call vnext. Whenever the name contains "loading_time" it generates a line of code that&lt;/P&gt;
&lt;P&gt;computes the corresponding difference. Note the "stop" at the end of the data step that prevents it&lt;/P&gt;
&lt;P&gt;to read further than the first observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input login_loading_time check_closing_time login_closing_time check_loading_time;
    cards;
400 415 456 421
405 214 413 411
410 412 400 403 
450 423 451 401
454 411 441 421
458 401 433 411
750 410 412 410
760 710 451 421
770 798 413 433
;
run;

data _NULL_;
    set have;
    length NAME $32.;

    call execute('data want; set have;');

    do while (NAME ne "NAME");
        call vnext(NAME);
        if find(upcase(NAME),"LOADING_TIME") then do;
            basename=scan(NAME,1,'_');
            call execute(cats(basename,'_diff=sum(',basename,'_loading_time,-',basename,'_closing_time);'));
			call execute('keep '||strip(basename)||'_diff;');
        end;
    end;
    call execute('run;');
    stop;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 18 Feb 2019 09:56:25 GMT</pubDate>
    <dc:creator>gamotte</dc:creator>
    <dc:date>2019-02-18T09:56:25Z</dc:date>
    <item>
      <title>How to substrate two variables by keywords in variable name?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-substrate-two-variables-by-keywords-in-variable-name/m-p/536003#M147235</link>
      <description>&lt;P&gt;I have a dataset such as the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;STRONG&gt;  login_loading_time    check_closing_time    login_closing_time    check_loading_time&lt;BR /&gt;    400                      415                   456                    421
    405                      214                   413                    411
    410                      412                   400                    403  
    450                      423                   451                    401
    454                      411                   441                    421
    458                      401                   433                    411
    750                      410                   412                    410
    760                      710                   451                    421
    770                      798                   413                    433&lt;/STRONG&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;I'm interested in finding out the difference between xxx_loading_time and xxx_closing_time (xxx_loading_time - xxx_closing_time). Here xxx can be 'check' or 'login', and the outcome dataset should look like the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;STRONG&gt;check_diff login_diff
  6             -56
 197            -8
  .              .
  .              .  &lt;/STRONG&gt;&lt;/PRE&gt;&lt;P&gt;Is there a way to index terms in SAS arithmetic operation. What will be the best way to achieve the outcome?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is much appriciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Feb 2019 19:49:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-substrate-two-variables-by-keywords-in-variable-name/m-p/536003#M147235</guid>
      <dc:creator>lydiawawa</dc:creator>
      <dc:date>2019-02-15T19:49:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to substrate two variables by keywords in variable name?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-substrate-two-variables-by-keywords-in-variable-name/m-p/536401#M147408</link>
      <description>&lt;P&gt;One possibility is to use a macro:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro diff(prefixes);
  %local i w;
  %do i=1 %to %sysfunc(countw(&amp;amp;prefixes));
    %let w=%scan(&amp;amp;prefixes,&amp;amp;i);
    &amp;amp;w._diff=&amp;amp;w._loading_time-&amp;amp;w._closing_time;
    drop &amp;amp;w._loading_time &amp;amp;w._closing_time;
    %end;
%mend;

data want;
  set have;
  %diff(login check);
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 18 Feb 2019 09:21:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-substrate-two-variables-by-keywords-in-variable-name/m-p/536401#M147408</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2019-02-18T09:21:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to substrate two variables by keywords in variable name?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-substrate-two-variables-by-keywords-in-variable-name/m-p/536424#M147417</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a solution where the code is generated by call execute's. It uses a loop to retrieve the names&lt;/P&gt;
&lt;P&gt;of the dataset's columns with call vnext. Whenever the name contains "loading_time" it generates a line of code that&lt;/P&gt;
&lt;P&gt;computes the corresponding difference. Note the "stop" at the end of the data step that prevents it&lt;/P&gt;
&lt;P&gt;to read further than the first observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input login_loading_time check_closing_time login_closing_time check_loading_time;
    cards;
400 415 456 421
405 214 413 411
410 412 400 403 
450 423 451 401
454 411 441 421
458 401 433 411
750 410 412 410
760 710 451 421
770 798 413 433
;
run;

data _NULL_;
    set have;
    length NAME $32.;

    call execute('data want; set have;');

    do while (NAME ne "NAME");
        call vnext(NAME);
        if find(upcase(NAME),"LOADING_TIME") then do;
            basename=scan(NAME,1,'_');
            call execute(cats(basename,'_diff=sum(',basename,'_loading_time,-',basename,'_closing_time);'));
			call execute('keep '||strip(basename)||'_diff;');
        end;
    end;
    call execute('run;');
    stop;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Feb 2019 09:56:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-substrate-two-variables-by-keywords-in-variable-name/m-p/536424#M147417</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2019-02-18T09:56:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to substrate two variables by keywords in variable name?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-substrate-two-variables-by-keywords-in-variable-name/m-p/536458#M147431</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30435"&gt;@lydiawawa&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This seems to be exactly the same question as your other question "Performing operation by keywords in varaible name", also posted friday? - It was&amp;nbsp;thoroughly discussed, and solutions given. I think they should be merged and closed.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Feb 2019 12:50:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-substrate-two-variables-by-keywords-in-variable-name/m-p/536458#M147431</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-02-18T12:50:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to substrate two variables by keywords in variable name?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-substrate-two-variables-by-keywords-in-variable-name/m-p/536736#M147540</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12887"&gt;@ErikLund_Jensen&lt;/a&gt; I'm not sure how the duplicated post was created. I will try to merge the two. Thanks for notifying!</description>
      <pubDate>Tue, 19 Feb 2019 14:34:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-substrate-two-variables-by-keywords-in-variable-name/m-p/536736#M147540</guid>
      <dc:creator>lydiawawa</dc:creator>
      <dc:date>2019-02-19T14:34:45Z</dc:date>
    </item>
  </channel>
</rss>

