<?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: HELP NEEDED ASAP: MACRO / Iterative &amp;quot;DO loop&amp;quot; to create a Matched Sample in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/MACRO-Iterative-quot-DO-loop-quot-to-create-a-Matched-Sample/m-p/275707#M58586</link>
    <description>&lt;P&gt;Thanks so much! I think this will work.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Follow-up question: Suppose I have a categorical variable that acts as an additional criteria besides the score. Say, gender.&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, I only want the matched pairs to be both male, or both female.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;At what point in your proposed code should I eliminate the combinations where GENDER_T ne GENDER_B ?&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 07 Jun 2016 15:18:40 GMT</pubDate>
    <dc:creator>wriccar</dc:creator>
    <dc:date>2016-06-07T15:18:40Z</dc:date>
    <item>
      <title>MACRO / Iterative "DO loop" to create a Matched Sample</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/MACRO-Iterative-quot-DO-loop-quot-to-create-a-Matched-Sample/m-p/275543#M58575</link>
      <description>&lt;P&gt;Hello fellow SAS users,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I hope someone can provide me with a bit of help. I am using SAS 9.3 and need to do something that is farily simple to understand, and I can certainly code it for a single iteration, but I need to do it many, many times.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please let me explain, simplifying the data set without sacrificing on the logic.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset with "TREATMENT" and "CONTROL" observations for which I am trying to form matched pairs based on a "penalty score," computed using data items that I already have.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have created a&amp;nbsp;data set that looks something like this, for simplicity:&lt;/P&gt;
&lt;P&gt;NAME_T &amp;nbsp; &amp;nbsp; ID_T &amp;nbsp; &amp;nbsp; VAR1_T &amp;nbsp; &amp;nbsp; VAR2_T &amp;nbsp; &amp;nbsp; VAR3_T &amp;nbsp; &amp;nbsp; &lt;SPAN&gt;NAME_C &amp;nbsp; &amp;nbsp; ID_C &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;VAR1_C &amp;nbsp; &amp;nbsp; VAR2_C &amp;nbsp; &amp;nbsp; VAR3_C&lt;/SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;NAME and ID are just identifying information;&lt;/P&gt;
&lt;P&gt;VAR1, VAR2, VAR3 = the data on which I will create the penalty score;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;_T = populated for treatment observations;&lt;/P&gt;
&lt;P&gt;_C = populated for control observations;&lt;/P&gt;
&lt;P&gt;(There is also a somewhat redundant variable, TREAT, which is =0,1 for control/treatment observations)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Suppose that I have &lt;STRONG&gt;1,000 TREATMENT&lt;/STRONG&gt; participants, and 10,000 &lt;U&gt;&lt;STRONG&gt;&lt;EM&gt;possible&lt;/EM&gt; &lt;/STRONG&gt;&lt;/U&gt;CONTROL participants included in the DATA file named "DATA_ALL"&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below, I have colored&amp;nbsp;the steps &lt;FONT color="#000080"&gt;in blue&lt;/FONT&gt; for which I am unable to execute a code, with notes. For other steps, I have annotated briefly.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Essentially, I need to do the following:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080"&gt;1a. Create a dataset with only 1 TREATMENT participant; call this SINGLE_TREATMENT&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080"&gt;(How do I reference ID=1 through 1,000 in single iterations?)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1b. Create a dataset with ALL control participants&lt;/P&gt;
&lt;P&gt;DATA CONTROL; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;SET DATA_ALL;&lt;/P&gt;
&lt;P&gt;IF TREAT=1 THEN DELETE;&lt;/P&gt;
&lt;P&gt;RUN;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. Merge the two using ("if _n_=1 then set SINGLE_TREATMENT") such that the &lt;STRONG&gt;single treatment participant&lt;/STRONG&gt; is "matched" with each available control participant&lt;/P&gt;
&lt;P&gt;DATA CONTROL_MATCHED;&lt;/P&gt;
&lt;P&gt;IF _N_=1 THEN SET SINGLE_TREATMENT;&lt;/P&gt;
&lt;P&gt;RUN;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3. Compute the "penalty score," for simplicity SCORE = (VAR1_T +&amp;nbsp;VAR2_T +&amp;nbsp;VAR3_T) - (VAR1_C + VAR2_C + VAR3_C), for each pair of participants&lt;/P&gt;
&lt;P&gt;DATA CONTROL_MATCHED;&lt;/P&gt;
&lt;P&gt;IF _N_=1 THEN SET SINGLE_TREATMENT;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;SCORE = (VAR1_T +&amp;nbsp;VAR2_T +&amp;nbsp;VAR3_T) - (VAR1_C + VAR2_C + VAR3_C)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;RUN;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;4. Sort the dataset by ascending "penalty score" so that the lowest&amp;nbsp;value of SCORE&amp;nbsp;is at the top&lt;/P&gt;
&lt;P&gt;PROC SORT DATA=CONTROL_MATCHED ASCENDING;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BY SCORE;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;RUN;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;***Steps 1 through 4 I can easily code, it is the repitition in Steps 5 through 8&amp;nbsp;that is difficult for me***&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;5. Delete all except the first observation, which will keep the pair with the lowest penalty score&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;&lt;SPAN&gt;6. Create a new DATA file for this matched pair&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;7. &lt;STRONG&gt;Repeat&lt;/STRONG&gt; this for each TREATMENT participant, 1,000 times&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;8. Merge all data sets created in Step 6, such that the final output has &lt;STRONG&gt;1,000 observations&lt;/STRONG&gt; (the number of TREATMENT participants), with each observation having identifying information for the best matched CONTROL observation&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;ANY HELP IS GREATLY APPRECIATED! THANK YOU IN ADVANCE!!!&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2016 10:48:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/MACRO-Iterative-quot-DO-loop-quot-to-create-a-Matched-Sample/m-p/275543#M58575</guid>
      <dc:creator>wriccar</dc:creator>
      <dc:date>2016-06-07T10:48:10Z</dc:date>
    </item>
    <item>
      <title>Re: HELP NEEDED ASAP: MACRO / Iterative "DO loop" to create a Matched Sample</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/MACRO-Iterative-quot-DO-loop-quot-to-create-a-Matched-Sample/m-p/275578#M58579</link>
      <description>&lt;P&gt;First, create datasets for treatment and control participants:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data
  treatment (keep=name_t id_t var1_t var2_t var3_t)
  control (keep=name_c id_c var1_c var2_c var3_c)
;
set data_all;
if treat = 1
then output treatment;
else output control;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then build a cartesian product (10 million obs should be no problem)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table inter as
select
  *,
  ((var1_t + var2_t + var3_t) - (var1_c + var2_c + var3_c)) as score
from
  treatment, control
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now sort and select&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=inter;
by name_t score;
run;

data want;
set inter;
by name_t;
if first.name_t;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;One can easily integrate the sort and select into the SQL, but I wanted to keep the steps simple&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2016 06:23:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/MACRO-Iterative-quot-DO-loop-quot-to-create-a-Matched-Sample/m-p/275578#M58579</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-06-07T06:23:28Z</dc:date>
    </item>
    <item>
      <title>Re: HELP NEEDED ASAP: MACRO / Iterative "DO loop" to create a Matched Sample</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/MACRO-Iterative-quot-DO-loop-quot-to-create-a-Matched-Sample/m-p/275707#M58586</link>
      <description>&lt;P&gt;Thanks so much! I think this will work.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Follow-up question: Suppose I have a categorical variable that acts as an additional criteria besides the score. Say, gender.&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, I only want the matched pairs to be both male, or both female.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;At what point in your proposed code should I eliminate the combinations where GENDER_T ne GENDER_B ?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2016 15:18:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/MACRO-Iterative-quot-DO-loop-quot-to-create-a-Matched-Sample/m-p/275707#M58586</guid>
      <dc:creator>wriccar</dc:creator>
      <dc:date>2016-06-07T15:18:40Z</dc:date>
    </item>
    <item>
      <title>Re: HELP NEEDED ASAP: MACRO / Iterative "DO loop" to create a Matched Sample</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/MACRO-Iterative-quot-DO-loop-quot-to-create-a-Matched-Sample/m-p/275751#M58593</link>
      <description>&lt;P&gt;In the SQL, you can add a where condition:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where a.gender_t = gender_t&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You must of course include the gender in the keeps of the first data step.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2016 17:31:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/MACRO-Iterative-quot-DO-loop-quot-to-create-a-Matched-Sample/m-p/275751#M58593</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-06-07T17:31:29Z</dc:date>
    </item>
  </channel>
</rss>

