<?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 merge and create new records ? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-and-create-new-records/m-p/819678#M323547</link>
    <description>&lt;P&gt;Seems you have an issue with the logic and you dropped the column paramcd, so i have&amp;nbsp;modified code and included below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data d1 ;&lt;BR /&gt;input VISIT PARAMCD $3-10 PATHOGEN $11-13 subject $14-16;&lt;BR /&gt;datalines ;&lt;BR /&gt;10 param3 P1 1&lt;BR /&gt;40 param1 P2 1&lt;BR /&gt;40 param3 P2 1&lt;BR /&gt;40 param2 P2 1&lt;BR /&gt;10 param1 P2 2&lt;BR /&gt;10 param3 P2 2&lt;BR /&gt;10 param2 P2 2&lt;BR /&gt;40 param1 P3 3&lt;BR /&gt;40 param3 P3 3&lt;BR /&gt;40 param2 P3 3&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;Data param1 param2 param3 ;&lt;BR /&gt;set d1 ;&lt;BR /&gt;if paramcd='param1' then output param1 ;&lt;BR /&gt;if paramcd='param2' then output param2 ;&lt;BR /&gt;if paramcd='param3' then output param3 ;&lt;/P&gt;
&lt;P&gt;run ;&lt;/P&gt;
&lt;P&gt;data d2 ;&lt;BR /&gt;merge d1 (in=a where= (visit ne . )) &lt;BR /&gt;param1 (in=param1 ) &lt;BR /&gt;param2 (in=param2 )&lt;BR /&gt;param3 (in=param3 ) ;&lt;BR /&gt;by subject visit pathogen ;&lt;BR /&gt;if paramcd='param1' and param1 then do ; paramcd= 'new1' ; output ;end; &lt;BR /&gt;if paramcd='param2' and param1 then do ; paramcd= 'new2' ; output ;end; &lt;BR /&gt;Run;&lt;/P&gt;</description>
    <pubDate>Wed, 22 Jun 2022 15:46:25 GMT</pubDate>
    <dc:creator>CarmineVerrell</dc:creator>
    <dc:date>2022-06-22T15:46:25Z</dc:date>
    <item>
      <title>How to merge and create new records ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-and-create-new-records/m-p/819670#M323544</link>
      <description>&lt;P&gt;I am trying to merge and create new records.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However with following code, the records for new2 are not output.&lt;/P&gt;
&lt;P&gt;I want records for new1 and new2 in the final data based on the conditions.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any solutions ? or may be sql alternative ?&lt;BR /&gt;Thanks.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data d1 ;
input VISIT  PARAMCD $3-10 PATHOGEN $11-13 subject $14-16;
datalines ;
10 param3 P1 1
40 param1 P2 1
40 param3 P2 1
40 param2 P2 1
10 param1 P2 2
10 param3 P2 2
10 param2 P2 2
40 param1 P3 3
40 param3 P3 3
40 param2 P3 3
;
run;

Data param1 param2 param3 ;
set d1 ;
		 if paramcd='param1' then output  param1 ;
		 if paramcd='param2' then output param2 ;
		 if paramcd='param3' then output param3 ;
drop paramcd ;
run ;

data d2 ;
	merge d1 (in=a where= (visit ne . )) 
		param1  (in=param1  ) 
		param2 (in=param2 )
		param3 (in=param3 )
	;by subject visit pathogen ;
		if paramcd='param1' and param2 then do ; paramcd= 'new1' ; output ;end; 
		if paramcd='param1' and param3 then do ; paramcd= 'new2' ; output ;end; 
Run;


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jun 2022 15:38:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-and-create-new-records/m-p/819670#M323544</guid>
      <dc:creator>chetan3125</dc:creator>
      <dc:date>2022-06-22T15:38:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge and create new records ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-and-create-new-records/m-p/819678#M323547</link>
      <description>&lt;P&gt;Seems you have an issue with the logic and you dropped the column paramcd, so i have&amp;nbsp;modified code and included below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data d1 ;&lt;BR /&gt;input VISIT PARAMCD $3-10 PATHOGEN $11-13 subject $14-16;&lt;BR /&gt;datalines ;&lt;BR /&gt;10 param3 P1 1&lt;BR /&gt;40 param1 P2 1&lt;BR /&gt;40 param3 P2 1&lt;BR /&gt;40 param2 P2 1&lt;BR /&gt;10 param1 P2 2&lt;BR /&gt;10 param3 P2 2&lt;BR /&gt;10 param2 P2 2&lt;BR /&gt;40 param1 P3 3&lt;BR /&gt;40 param3 P3 3&lt;BR /&gt;40 param2 P3 3&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;Data param1 param2 param3 ;&lt;BR /&gt;set d1 ;&lt;BR /&gt;if paramcd='param1' then output param1 ;&lt;BR /&gt;if paramcd='param2' then output param2 ;&lt;BR /&gt;if paramcd='param3' then output param3 ;&lt;/P&gt;
&lt;P&gt;run ;&lt;/P&gt;
&lt;P&gt;data d2 ;&lt;BR /&gt;merge d1 (in=a where= (visit ne . )) &lt;BR /&gt;param1 (in=param1 ) &lt;BR /&gt;param2 (in=param2 )&lt;BR /&gt;param3 (in=param3 ) ;&lt;BR /&gt;by subject visit pathogen ;&lt;BR /&gt;if paramcd='param1' and param1 then do ; paramcd= 'new1' ; output ;end; &lt;BR /&gt;if paramcd='param2' and param1 then do ; paramcd= 'new2' ; output ;end; &lt;BR /&gt;Run;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jun 2022 15:46:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-and-create-new-records/m-p/819678#M323547</guid>
      <dc:creator>CarmineVerrell</dc:creator>
      <dc:date>2022-06-22T15:46:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge and create new records ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-and-create-new-records/m-p/819681#M323548</link>
      <description>&lt;P&gt;Please look at the result from this code:&lt;/P&gt;
&lt;PRE&gt;data d2 ;
	merge d1 (in=a where= (visit ne . )) 
		param1  (in=param1  ) 
		param2 (in=param2 )
		param3 (in=param3 )
	;by subject visit pathogen ;
		if paramcd='param1' and param2 then do ; paramcdnew= 'new1' ; output ;end; 
		if paramcd='param1' and param3 then do ; paramcdnew= 'new2' ; output ;end; 
Run;&lt;/PRE&gt;
&lt;P&gt;At least with your example data, if I understand what you are attempting, when you set Paramcd to 'new1' the value is no longer there for use in the second IF.&lt;/P&gt;
&lt;P&gt;Try this: Test the Paramcd value only one time.&lt;/P&gt;
&lt;PRE&gt;data d2 ;
	merge d1 (in=a where= (visit ne . )) 
		param1  (in=param1  ) 
		param2 (in=param2 )
		param3 (in=param3 )
	;by subject visit pathogen ;
	if paramcd='param1' Then do;
           if param2 then do ; paramcd= 'new1' ; output ;end; 
	   if param3 then do ; paramcd= 'new2' ; output ;end; 
       end;
Run;

&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Jun 2022 15:56:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-and-create-new-records/m-p/819681#M323548</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-06-22T15:56:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge and create new records ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-and-create-new-records/m-p/819703#M323553</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;This worked. Thanks. Also changed the output var name.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jun 2022 16:29:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-and-create-new-records/m-p/819703#M323553</guid>
      <dc:creator>chetan3125</dc:creator>
      <dc:date>2022-06-22T16:29:01Z</dc:date>
    </item>
  </channel>
</rss>

