<?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 refer to columns in a particular dataset in a DATA STEP merge? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-refer-to-columns-in-a-particular-dataset-in-a-DATA-STEP/m-p/451241#M113729</link>
    <description>&lt;P&gt;In this case since PLACE only exists in one data set there is no need to attempt to specify the set, it is already in the data vector as the variable Place. Just use: &lt;SPAN class="token statement"&gt;where&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;prxmatch&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'/^a/i'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; place&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming that you actually meant (otherwise both data sets contributed the same in variable)&lt;/P&gt;
&lt;PRE&gt;data foo;
	merge a(in=a) b(in=b);
	by id;
	where prxmatch('/^a/i', b.place);
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;MERGE combines the records before the executable steps following the merge are executed. So the concept of a variable belonging to as&amp;nbsp;specific data&amp;nbsp;set is gone.&lt;/P&gt;
&lt;P&gt;You can reference records that contributed to the current record with&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If b then &amp;lt;do something&amp;gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However to see a real difference between data step merge and sql join use this data b:&lt;/P&gt;
&lt;PRE&gt;data b;
	input id place $ variety $;
	datalines;
1 binghamton  a
2 westchester b
3 scranton    c
4 utica       d
5 albany      e
;
run;
&lt;/PRE&gt;
&lt;P&gt;and see what happens in data foo.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then do the same data foo but reverse the order of a and b on the merge statement.&lt;/P&gt;</description>
    <pubDate>Wed, 04 Apr 2018 18:27:48 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2018-04-04T18:27:48Z</dc:date>
    <item>
      <title>How to refer to columns in a particular dataset in a DATA STEP merge?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-refer-to-columns-in-a-particular-dataset-in-a-DATA-STEP/m-p/451230#M113727</link>
      <description>&lt;P&gt;These are my test datasets:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
	input id variety $;
	datalines;
1 jazz
2 gala
3 empire
4 rome
5 mcintosh
;
run;

data b;
	input id place $;
	datalines;
1 binghamton
2 westchester
3 scranton
4 utica
5 albany
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Sorting boilerplate:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=a; by id; run;
proc sort data=b; by id; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I'm trying to do something like this but with the DATA STEP:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table foo as 
	select *
	from a join b on a.id = b.id 
	where prxmatch('/^a/i', b.place);
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This is my attempt but it throws syntax errors:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data foo;
	merge a(in=a) b(in=a);
	by id;
	where prxmatch('/^a/i', b.place);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;How would I refer to b.place in the data step?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 18:09:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-refer-to-columns-in-a-particular-dataset-in-a-DATA-STEP/m-p/451230#M113727</guid>
      <dc:creator>tomcmacdonald</dc:creator>
      <dc:date>2018-04-04T18:09:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to refer to columns in a particular dataset in a DATA STEP merge?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-refer-to-columns-in-a-particular-dataset-in-a-DATA-STEP/m-p/451241#M113729</link>
      <description>&lt;P&gt;In this case since PLACE only exists in one data set there is no need to attempt to specify the set, it is already in the data vector as the variable Place. Just use: &lt;SPAN class="token statement"&gt;where&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;prxmatch&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'/^a/i'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; place&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming that you actually meant (otherwise both data sets contributed the same in variable)&lt;/P&gt;
&lt;PRE&gt;data foo;
	merge a(in=a) b(in=b);
	by id;
	where prxmatch('/^a/i', b.place);
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;MERGE combines the records before the executable steps following the merge are executed. So the concept of a variable belonging to as&amp;nbsp;specific data&amp;nbsp;set is gone.&lt;/P&gt;
&lt;P&gt;You can reference records that contributed to the current record with&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If b then &amp;lt;do something&amp;gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However to see a real difference between data step merge and sql join use this data b:&lt;/P&gt;
&lt;PRE&gt;data b;
	input id place $ variety $;
	datalines;
1 binghamton  a
2 westchester b
3 scranton    c
4 utica       d
5 albany      e
;
run;
&lt;/PRE&gt;
&lt;P&gt;and see what happens in data foo.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then do the same data foo but reverse the order of a and b on the merge statement.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 18:27:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-refer-to-columns-in-a-particular-dataset-in-a-DATA-STEP/m-p/451241#M113729</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-04-04T18:27:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to refer to columns in a particular dataset in a DATA STEP merge?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-refer-to-columns-in-a-particular-dataset-in-a-DATA-STEP/m-p/451243#M113730</link>
      <description>&lt;P&gt;This is the error message I get when executing your first code snippet:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ERROR: Variable place is not on file WORK.A.&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 04 Apr 2018 18:33:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-refer-to-columns-in-a-particular-dataset-in-a-DATA-STEP/m-p/451243#M113730</guid>
      <dc:creator>tomcmacdonald</dc:creator>
      <dc:date>2018-04-04T18:33:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to refer to columns in a particular dataset in a DATA STEP merge?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-refer-to-columns-in-a-particular-dataset-in-a-DATA-STEP/m-p/451255#M113732</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/142145"&gt;@tomcmacdonald&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;This is the error message I get when executing your first code snippet:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ERROR: Variable place is not on file WORK.A.&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The purpose of the WHERE statement is to limit the data that is read into the data step.&amp;nbsp; If you use a WHERE statement in a data step it is applied to all of the input dataset.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to limit the observations that are written out you&amp;nbsp;could just convert to a subsetting&amp;nbsp;IF statement instead or&amp;nbsp;convert the logic to use DELETE or OUTPUT statements to determine which observations are written out.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge A B ;
  by ID ;
  if (....) ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or you could use the WHERE= dataset option on the dataset it applies to to limit the records read from that dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge A B(where=(....)) ;
  by ID ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 19:03:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-refer-to-columns-in-a-particular-dataset-in-a-DATA-STEP/m-p/451255#M113732</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-04-04T19:03:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to refer to columns in a particular dataset in a DATA STEP merge?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-refer-to-columns-in-a-particular-dataset-in-a-DATA-STEP/m-p/451342#M113757</link>
      <description>&lt;P&gt;Please show code executed. I did not intend you to run&lt;/P&gt;
&lt;PRE&gt;data foo;
	merge a(in=a) b(in=b);
	by id;
	where prxmatch('/^a/i', b.place);
run;&lt;/PRE&gt;
&lt;P&gt;because the b.place is an error, that was to point out you had used (in=a) for both set a and b.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Executable code would be&lt;/P&gt;
&lt;PRE&gt;data foo;
	merge a b;
	by id;
	where prxmatch('/^a/i',place);
run;&lt;/PRE&gt;
&lt;P&gt;Since the In variables really weren't of use. And assumes the prxmatch is valid.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 22:56:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-refer-to-columns-in-a-particular-dataset-in-a-DATA-STEP/m-p/451342#M113757</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-04-04T22:56:06Z</dc:date>
    </item>
  </channel>
</rss>

