<?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 How to merge to simiar code in SAS in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-to-simiar-code-in-SAS/m-p/670389#M201235</link>
    <description>&lt;P&gt;Hi community,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have two indistinguishable sets of code for solving data for two different variables (the codes for each set includes: proc sort, proc means, merge, proc rank, and trim data), &lt;STRONG&gt;the order of the lines of code and everything is similar between these two sets of codes (the difference is only the name of the variable)&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can get to the goal with these two sets of code, but since I saw the aesthetic SAS, I believe that there should be one way to merge these codes (especially they are resemblant to each other).&amp;nbsp; I am wondering if we can merge these two sets of code together. I am wondering if you can suggest me the way that I can search or follow.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance.&lt;/P&gt;
&lt;DIV id="eJOY__extension_root" class="eJOY__extension_root_class" style="all: unset;"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
    <pubDate>Sat, 18 Jul 2020 21:21:54 GMT</pubDate>
    <dc:creator>Phil_NZ</dc:creator>
    <dc:date>2020-07-18T21:21:54Z</dc:date>
    <item>
      <title>How to merge to simiar code in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-to-simiar-code-in-SAS/m-p/670389#M201235</link>
      <description>&lt;P&gt;Hi community,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have two indistinguishable sets of code for solving data for two different variables (the codes for each set includes: proc sort, proc means, merge, proc rank, and trim data), &lt;STRONG&gt;the order of the lines of code and everything is similar between these two sets of codes (the difference is only the name of the variable)&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can get to the goal with these two sets of code, but since I saw the aesthetic SAS, I believe that there should be one way to merge these codes (especially they are resemblant to each other).&amp;nbsp; I am wondering if we can merge these two sets of code together. I am wondering if you can suggest me the way that I can search or follow.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance.&lt;/P&gt;
&lt;DIV id="eJOY__extension_root" class="eJOY__extension_root_class" style="all: unset;"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Sat, 18 Jul 2020 21:21:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-to-simiar-code-in-SAS/m-p/670389#M201235</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2020-07-18T21:21:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge to simiar code in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-to-simiar-code-in-SAS/m-p/670391#M201237</link>
      <description>&lt;P&gt;I'm not aware of any way to "merge code" other than by applying human intelligence and effort to the process.&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jul 2020 21:56:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-to-simiar-code-in-SAS/m-p/670391#M201237</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-07-18T21:56:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge to simiar code in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-to-simiar-code-in-SAS/m-p/670392#M201238</link>
      <description>You would want to use a sas macro&lt;BR /&gt;%macro mycode(indsname, outdsname);&lt;BR /&gt;&amp;lt; your code&amp;gt;;&lt;BR /&gt;%mend mycode;&lt;BR /&gt;%mycode;&lt;BR /&gt;The thing you have to remember that the input to the first step would be indsname. For example if you use a data step as your first step then &lt;BR /&gt;Data new;&lt;BR /&gt;Set &amp;amp;indsname;&lt;BR /&gt;Run;&lt;BR /&gt;If your first step is a proc then&lt;BR /&gt;Proc &amp;lt;name of proc&amp;gt; data=&amp;amp;indsname.&lt;BR /&gt;&lt;BR /&gt;On the same note for output if your last step is a datastep then&lt;BR /&gt;Data &amp;amp;outdsname.;&lt;BR /&gt;Set &amp;lt;some data set&amp;gt;;&lt;BR /&gt;Run;&lt;BR /&gt;If your last step is a proc then you specify the outdsname on out= line as shown below&lt;BR /&gt; out = &amp;amp;outdsname.</description>
      <pubDate>Sat, 18 Jul 2020 22:01:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-to-simiar-code-in-SAS/m-p/670392#M201238</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2020-07-18T22:01:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge to simiar code in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-to-simiar-code-in-SAS/m-p/670393#M201239</link>
      <description>&lt;P&gt;This might be a case for macro processing, but we will need to see those codes first.&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jul 2020 22:11:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-to-simiar-code-in-SAS/m-p/670393#M201239</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-18T22:11:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge to simiar code in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-to-simiar-code-in-SAS/m-p/670394#M201240</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;, one part of my code is as below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options compress=yes reuse=yes;
data finish_error;
set 'D:\link_to_the_dataset';
run;
proc sort data=finish_error;
by Type date;
run;
data amihud_;
set finish_error;
by Type;
amihud=abs_r/trading_vol;
year=year(date);
if first.type then trading_vol=. and amihud=.;
run;
.
.
.
proc rank data=create_var groups=100 out=temp;
by year;
var lag_p_us;
ranks rank;
run;

data trim_price;
set temp;
if 0 &amp;lt; rank &amp;lt; 99;
run;
proc sort data=trim_price;
by Type;
run;
data amihud_final (drop= p_us lag_p_us rank rename=(obs=obs_amihud));
set trim_price;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thank you!&lt;/P&gt;
&lt;DIV id="eJOY__extension_root" class="eJOY__extension_root_class" style="all: unset;"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Sat, 18 Jul 2020 22:22:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-to-simiar-code-in-SAS/m-p/670394#M201240</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2020-07-18T22:22:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge to simiar code in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-to-simiar-code-in-SAS/m-p/670395#M201241</link>
      <description>&lt;P&gt;And this code is for the variable amihud, I have a similar code for another variable named "b". The code for dealing with them are similar, just different in calculation here:&lt;BR /&gt;for amihud:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data amihud_;
set finish_error;
by Type;
amihud=abs_r/trading_vol;
year=year(date);
if first.type then trading_vol=. and amihud=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;for b:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data closing_;
set finish_error;
by Type;
b=divide(pa_us-pb_us,mean_pa_pb);
if b&amp;gt; 0.5*mean_pa_pb then b=.;
year=year(date);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Other codes in these two sets are indistinguishable.&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;DIV id="eJOY__extension_root" class="eJOY__extension_root_class" style="all: unset;"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Sat, 18 Jul 2020 22:26:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-to-simiar-code-in-SAS/m-p/670395#M201241</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2020-07-18T22:26:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge to simiar code in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-to-simiar-code-in-SAS/m-p/670397#M201242</link>
      <description>&lt;P&gt;Is the question how to create both variables in one data step?&lt;/P&gt;
&lt;P&gt;If so it looks like it should be possible.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that your first data step has either a mistake or a very strange syntax that should probably be commented to explain to yourself what it means.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data amihud_closing;
  set finish_error;
  by Type;
  year=year(date);
* Calculate AMIHUD ;
  amihud=abs_r/trading_vol;
 * if first.type then trading_vol=. and amihud=.;
  if first.type then call missing(trading_vol,amihud);
* Calculate B ;
  b=divide(pa_us-pb_us,mean_pa_pb);
  if b&amp;gt; 0.5*mean_pa_pb then b=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 18 Jul 2020 22:44:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-to-simiar-code-in-SAS/m-p/670397#M201242</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-18T22:44:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge to simiar code in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-to-simiar-code-in-SAS/m-p/670406#M201244</link>
      <description>&lt;P&gt;If you have a lot of common code preceding or following these two distinct data step, then you could embed them in a macro definition as below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mytask(dsn=);

  **** preceding common code here *****;

  %if &amp;amp;dsn=amihud %then %do;
    data amihud_;
      set finish_error;
      by Type;
      amihud=abs_r/trading_vol;
      year=year(date);
      if first.type then trading_vol=. and amihud=.;
    run;
  %end;
  %else %if &amp;amp;dsn=closing %then %do;
    data closing_;
      set finish_error;
      by Type;
      b=divide(pa_us-pb_us,mean_pa_pb);
      if b&amp;gt; 0.5*mean_pa_pb then b=.;
      year=year(date);
    run;
  %end;

  **** trailing common code here *****;

%mend mytask;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note I assume that these two distinct steps occur in precisely the same relative position within the common code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then all you need to do is invoke the macro based on your needs.&amp;nbsp; Below the macro is invoked twice, once for each distinct code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%mytask(dsn=amihud);
%mytask(dsn=closing);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jul 2020 23:00:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-to-simiar-code-in-SAS/m-p/670406#M201244</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-07-18T23:00:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge to simiar code in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-to-simiar-code-in-SAS/m-p/670407#M201245</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;, can you please quote the data step that you feel it is strange, I am going to have a look at it carefully. Thanks&lt;/P&gt;
&lt;DIV id="eJOY__extension_root" class="eJOY__extension_root_class" style="all: unset;"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Sat, 18 Jul 2020 23:05:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-to-simiar-code-in-SAS/m-p/670407#M201245</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2020-07-18T23:05:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge to simiar code in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-to-simiar-code-in-SAS/m-p/670409#M201247</link>
      <description>Or do you mean this one?&lt;BR /&gt;data finish_error;&lt;BR /&gt;set 'D:\link_to_the_dataset';&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;This is how I call my dataset using the SAS EG.</description>
      <pubDate>Sat, 18 Jul 2020 23:49:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-to-simiar-code-in-SAS/m-p/670409#M201247</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2020-07-18T23:49:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge to simiar code in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-to-simiar-code-in-SAS/m-p/670457#M201271</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/212695"&gt;@Phil_NZ&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;, can you please quote the data step that you feel it is strange, I am going to have a look at it carefully. Thanks&lt;/P&gt;
&lt;DIV id="eJOY__extension_root" class="eJOY__extension_root_class" style="all: unset;"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I left the line in the code, only as a comment.&amp;nbsp; This statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;trading_vol=. and amihud=.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is setting TRADING_VOL to zero.&amp;nbsp; SAS evaluates boolean expressions as either zero or one and neither one will be considered equal to missing so the result is always zero.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I replaced it with:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;call missing(trading_vol,amihud);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which will set both TRADING_VOL and AMIHUD to missing.&lt;/P&gt;</description>
      <pubDate>Sun, 19 Jul 2020 14:48:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-to-simiar-code-in-SAS/m-p/670457#M201271</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-19T14:48:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge to simiar code in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-to-simiar-code-in-SAS/m-p/670502#M201297</link>
      <description>&lt;P&gt;Awesome, thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp; for this comprehensive explanation&lt;/P&gt;
&lt;DIV id="eJOY__extension_root" class="eJOY__extension_root_class" style="all: unset;"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Sun, 19 Jul 2020 19:53:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-to-simiar-code-in-SAS/m-p/670502#M201297</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2020-07-19T19:53:08Z</dc:date>
    </item>
  </channel>
</rss>

