<?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: merging datasets in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/merging-datasets/m-p/826101#M35257</link>
    <description>&lt;UL&gt;
&lt;LI&gt;rename the month variables so that they are the same&lt;/LI&gt;
&lt;LI&gt;add the month variable to by in proc sort and data step&lt;/LI&gt;
&lt;LI&gt;remove the if statement&lt;/LI&gt;
&lt;LI&gt;add the in option to both datasets in the merge statement:&lt;/LI&gt;
&lt;/UL&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;merge inputdata1(in=in1) inputdata2(in=in2);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;UL&gt;
&lt;LI&gt;add: if in1 and in2; to the data step&lt;/LI&gt;
&lt;/UL&gt;</description>
    <pubDate>Fri, 29 Jul 2022 06:21:02 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2022-07-29T06:21:02Z</dc:date>
    <item>
      <title>merging datasets</title>
      <link>https://communities.sas.com/t5/New-SAS-User/merging-datasets/m-p/826098#M35256</link>
      <description>&lt;P&gt;Hi, I want to extract some rows in one dataset(inputdata1) based on information from another datasets(inputdata2).&amp;nbsp;&lt;/P&gt;&lt;P&gt;inputdata1 has variables id and month1, and&amp;nbsp;inputdata2 has id, month2, and cost. What I want to do is when it is the same id in inputdata1 and inputdata2, if month2 equals month1, then output. However, the total number of month for the same id is different in&amp;nbsp;inputdata1 and&amp;nbsp;inputdata2. And the size of these two datasets are different. I'm not sure if SAS is able to do what I want. I insert the codes below. The data want in the bottom is what I want when I have&amp;nbsp;inputdata1 and&amp;nbsp;inputdata2. The rest codes are my attempt, but it didn't output what I want.&lt;/P&gt;&lt;P&gt;Any suggestions would be very appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data inputdata1;
	input id $ month1;
	datalines;
	637 1 
	637 2 
	637 3
	637 4
	637 5
	675 1
	675 2
	675 3
	675 4
	675 5
	675 6
	675 7
	675 8
	675 9
	891 1
	891 2
	891 3
	;
run;
proc print data= inputdata1;
run;

data inputdata2;
	input id $ month2 sum;
	datalines;
	637 3 28.9
	637 7 34.7
	675 1 34.6
	675 4 35.7
	891 9 33.4
	;
run;
proc print data= inputdata2;
run;

proc sort data=inputdata1 out=inputdata1;
	by id;
run;
proc sort data=inputdata2 out=inputdata2;
	by id;
run;

data combine;
	merge inputdata1 inputdata2;
	by id;
	if month1=month2;
run;
proc print data=combine;
run;

data want;
	input id $ month2 cost;
	datalines;
	637 3 28.9
	675 1 34.6
	675 4 35.7
	;


&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jul 2022 03:15:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/merging-datasets/m-p/826098#M35256</guid>
      <dc:creator>LisaZ1</dc:creator>
      <dc:date>2022-07-29T03:15:43Z</dc:date>
    </item>
    <item>
      <title>Re: merging datasets</title>
      <link>https://communities.sas.com/t5/New-SAS-User/merging-datasets/m-p/826101#M35257</link>
      <description>&lt;UL&gt;
&lt;LI&gt;rename the month variables so that they are the same&lt;/LI&gt;
&lt;LI&gt;add the month variable to by in proc sort and data step&lt;/LI&gt;
&lt;LI&gt;remove the if statement&lt;/LI&gt;
&lt;LI&gt;add the in option to both datasets in the merge statement:&lt;/LI&gt;
&lt;/UL&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;merge inputdata1(in=in1) inputdata2(in=in2);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;UL&gt;
&lt;LI&gt;add: if in1 and in2; to the data step&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Fri, 29 Jul 2022 06:21:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/merging-datasets/m-p/826101#M35257</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-07-29T06:21:02Z</dc:date>
    </item>
    <item>
      <title>Re: merging datasets</title>
      <link>https://communities.sas.com/t5/New-SAS-User/merging-datasets/m-p/826114#M35261</link>
      <description>&lt;P&gt;Good day&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You're welcome to try this alternative, using PROC SQL. Quite useful when starting to merge datasets.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sql;
	create table want as 
		select *
			from inputdata1 as a
				inner join inputdata2 as b
					on (a.id=b.id and a.month1=b.month2);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Jul 2022 07:59:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/merging-datasets/m-p/826114#M35261</guid>
      <dc:creator>Adriaan_Gouws</dc:creator>
      <dc:date>2022-07-29T07:59:32Z</dc:date>
    </item>
    <item>
      <title>Re: merging datasets</title>
      <link>https://communities.sas.com/t5/New-SAS-User/merging-datasets/m-p/826206#M35267</link>
      <description>hi, I tried to modify my codes according to what you suggest, but it does not output what I want, either. However, I tried using proc sql and it works. Thank you anyway!</description>
      <pubDate>Fri, 29 Jul 2022 19:03:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/merging-datasets/m-p/826206#M35267</guid>
      <dc:creator>LisaZ1</dc:creator>
      <dc:date>2022-07-29T19:03:30Z</dc:date>
    </item>
    <item>
      <title>Re: merging datasets</title>
      <link>https://communities.sas.com/t5/New-SAS-User/merging-datasets/m-p/826207#M35268</link>
      <description>Thank you for you suggestion. It works. It outputs exactly what I want! Thank you!</description>
      <pubDate>Fri, 29 Jul 2022 19:04:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/merging-datasets/m-p/826207#M35268</guid>
      <dc:creator>LisaZ1</dc:creator>
      <dc:date>2022-07-29T19:04:13Z</dc:date>
    </item>
  </channel>
</rss>

