<?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: Merge Datasets With a Conditional Drop Statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merge-Datasets-With-a-Conditional-Drop-Statement/m-p/899863#M355642</link>
    <description>&lt;P&gt;You should get in the habit of including RUN (or QUIT where appropriate) to help the humans that have to read the code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data combined;
  merge TMP1.HW10Demo (in=inD) TMP2.HW10Baseline (in=inB);
  by Site;
  if inD and inB then Complete=11;
  else if inD and ~inB then Complete=10;
  else Complete=01;
  Age=yrdif(DOB,Entry,'act/act');
  TTime=datdif(Entry,Test,'act/act');
run;

data DBF;
  merge combined TMP3.HW10Followup;
  FupTime=datdif(Entry,fupdt,'act/act');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could write THREE datasets in the last data step.&amp;nbsp; That is the only way to conditionally change the number of variables.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data SID1(keep=Site ID fupdt fuptest Test Score) 
     SID2(drop=fupdt fuptest Test Score)
     SID3
;
  merge TMP1.HW10Baseline (in=inB) TMP3.HW10Followup (in=inF);
  by Site;
  if inB and inF then output sid1 ; 
  else if inB and ~inF then output sid2; 
  else output sid3;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 25 Oct 2023 02:11:48 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-10-25T02:11:48Z</dc:date>
    <item>
      <title>Merge Datasets With a Conditional Drop Statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-Datasets-With-a-Conditional-Drop-Statement/m-p/899857#M355639</link>
      <description>&lt;PRE&gt;&lt;CODE class=""&gt;data work.combined;
	merge TMP1.HW10Demo (in=inD) TMP2.HW10Baseline (in=inB);
	by Site;
	if inD and inB then Complete=11;
	else if inD and ~inB then Complete=10;
	else Complete=01;
	Age=yrdif(DOB,Entry,'act/act');
	TTime=datdif(Entry,Test,'act/act');
data work.DBF;
	merge work.combined TMP3.HW10Followup;
	FupTime=datdif(Entry,fupdt,'act/act');
data work.SID;
	merge TMP1.HW10Baseline (in=inB) TMP3.HW10Followup (in=inF);
	by Site;
	if inB and inF then keep Site ID fupdt fuptest Test Score;
	else if inB and ~inF then drop fupdt fuptest Test Score;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;My last data step I am trying to merge those two sets and only keep variables that have both Baseline and Follow Up data in the new set. I am getting an error that says my variables have never been referenced. Help please!&lt;/P&gt;</description>
      <pubDate>Tue, 24 Oct 2023 22:57:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-Datasets-With-a-Conditional-Drop-Statement/m-p/899857#M355639</guid>
      <dc:creator>madtex99</dc:creator>
      <dc:date>2023-10-24T22:57:11Z</dc:date>
    </item>
    <item>
      <title>Re: Merge Datasets With a Conditional Drop Statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-Datasets-With-a-Conditional-Drop-Statement/m-p/899858#M355640</link>
      <description>&lt;P&gt;You wil need to re-think what you are trying to do here.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First, in a SAS data set, all variables exist on every observation.&amp;nbsp; There is no possibility of keeping (or dropping) variables differently from one observation to the next.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second, KEEP or DROP (no matter how and where specified) cannot be executed conditionally.&amp;nbsp; They cannnot be part of an IF THEN statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Theoretically possible if it meets your needs:&amp;nbsp; You can create two output data sets.&amp;nbsp; You could keep a different set of variables in each output data set.&amp;nbsp; And you might use the in= variables to determine which observations get output to which data sets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 24 Oct 2023 23:05:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-Datasets-With-a-Conditional-Drop-Statement/m-p/899858#M355640</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-10-24T23:05:35Z</dc:date>
    </item>
    <item>
      <title>Re: Merge Datasets With a Conditional Drop Statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-Datasets-With-a-Conditional-Drop-Statement/m-p/899862#M355641</link>
      <description>&lt;P&gt;To add to what&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;wrote.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SAS tables are rectangular and each observation (row) will have the same set of variables (columns).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SAS variables (the program data vector) get defined during the compilation phase of a data step which is another reason why you can't drop them conditionally during the execution phase.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1698198398956.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89064iADC49B8599B82A35/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1698198398956.png" alt="Patrick_0-1698198398956.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/basess/n053a58fwk57v7n14h8x7y7u34y4.htm" target="_self"&gt;Overview of the DATA Step&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To understand the distinction between compilation and execution phase is important and will help you with defining program logic.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Statements like length, attrib, array, retain etc. are all on compilation level and though can't get used conditionally during the execution phase.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Think of the compilation phase as an iteration zero where the SAS compiler scans through your code and prepares everything for execution.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2023 01:50:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-Datasets-With-a-Conditional-Drop-Statement/m-p/899862#M355641</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-10-25T01:50:54Z</dc:date>
    </item>
    <item>
      <title>Re: Merge Datasets With a Conditional Drop Statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-Datasets-With-a-Conditional-Drop-Statement/m-p/899863#M355642</link>
      <description>&lt;P&gt;You should get in the habit of including RUN (or QUIT where appropriate) to help the humans that have to read the code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data combined;
  merge TMP1.HW10Demo (in=inD) TMP2.HW10Baseline (in=inB);
  by Site;
  if inD and inB then Complete=11;
  else if inD and ~inB then Complete=10;
  else Complete=01;
  Age=yrdif(DOB,Entry,'act/act');
  TTime=datdif(Entry,Test,'act/act');
run;

data DBF;
  merge combined TMP3.HW10Followup;
  FupTime=datdif(Entry,fupdt,'act/act');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could write THREE datasets in the last data step.&amp;nbsp; That is the only way to conditionally change the number of variables.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data SID1(keep=Site ID fupdt fuptest Test Score) 
     SID2(drop=fupdt fuptest Test Score)
     SID3
;
  merge TMP1.HW10Baseline (in=inB) TMP3.HW10Followup (in=inF);
  by Site;
  if inB and inF then output sid1 ; 
  else if inB and ~inF then output sid2; 
  else output sid3;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2023 02:11:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-Datasets-With-a-Conditional-Drop-Statement/m-p/899863#M355642</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-10-25T02:11:48Z</dc:date>
    </item>
  </channel>
</rss>

