<?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: Insert a unique record within macro in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Insert-a-unique-record-within-macro/m-p/429219#M68556</link>
    <description>&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 19 Jan 2018 18:07:04 GMT</pubDate>
    <dc:creator>Crubal</dc:creator>
    <dc:date>2018-01-19T18:07:04Z</dc:date>
    <item>
      <title>Insert a unique record within macro</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Insert-a-unique-record-within-macro/m-p/428936#M68530</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a question to insert a record to table 'sas_gbl_input_parm' within macro function.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The table has 4 columns: parm_nm, value, location, description.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro setrec (parm_nm, value, location, desc);

%if %length(&amp;amp;location)=0 %then %let location=_ALL_;
	%let default_text=;
	%let locid_text=;

	proc sql noprint;

		select parm_val into :default_text from sas_gbl_input_parm
			where upcase(parm_nm)=upcase("&amp;amp;parm_nm")
			  and current_ind = 'Y'
			;
	quit;

	proc sql noprint;

/* Insert a record to previous table no matter parm_nm exists */

	%if &amp;amp;location ne _ALL_ and %length(&amp;amp;locid_text)=0
	   %then %do; 
					insert into sas_gbl_input_parm
						set parm_nm = "&amp;amp;parm_nm"
						  , htl_cd = "&amp;amp;location"
				   		  , parm_val = "&amp;amp;value"
						  , current_ind = 'Y'
						  , lst_updt_usr_id = "User_1"
						  , lst_updt_ts = datetime()
					;
	         %end;&lt;BR /&gt;        quit;&lt;BR /&gt;&lt;BR /&gt;%mend setrec;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;However, in this way, we will insert a record whether initial 'parm_nm' exists or not;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I modify the code so that if the table already contains 'parm_nm' of the new record, it will just update the value of initial record; and if the table does not contain 'parm_nm' of the new record, it will insert a new&amp;nbsp;row&amp;nbsp;of 4 columns to prior table.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jan 2018 21:30:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Insert-a-unique-record-within-macro/m-p/428936#M68530</guid>
      <dc:creator>Crubal</dc:creator>
      <dc:date>2018-01-18T21:30:17Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a unique record within macro</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Insert-a-unique-record-within-macro/m-p/428940#M68531</link>
      <description>&lt;P&gt;Do you mean you have an &lt;STRONG&gt;UPSERT&lt;/STRONG&gt; situation?&lt;/P&gt;&lt;P&gt;If yes, you are better off pushing the query to in database processing rather than doing at SAS level, which i think is rather tedious.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Suppose your database is oracle, you have excellent options deal with UPSERT situations. I guess for OLTP processing, bringing to SAS environment is prolly not the best practice. My 2 cents&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This link does have a SAS pseudo code if you wanna have a look&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://stackoverflow.com/questions/11743778/similar-statement-to-merge-sql-in-sas" target="_blank"&gt;https://stackoverflow.com/questions/11743778/similar-statement-to-merge-sql-in-sas&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jan 2018 22:00:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Insert-a-unique-record-within-macro/m-p/428940#M68531</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-01-18T22:00:40Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a unique record within macro</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Insert-a-unique-record-within-macro/m-p/428944#M68532</link>
      <description>&lt;P&gt;Thanks! As you mentioned it is a table from Oracle database. I will look into the link you sent to me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The reason why I am using SAS macro is because this is part of the product (together with other code) and the rest is all SAS code. So that I would like to make them consistent.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jan 2018 22:17:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Insert-a-unique-record-within-macro/m-p/428944#M68532</guid>
      <dc:creator>Crubal</dc:creator>
      <dc:date>2018-01-18T22:17:20Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a unique record within macro</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Insert-a-unique-record-within-macro/m-p/429048#M68535</link>
      <description>&lt;P&gt;One possibility would be to try the update first, and check if any observations were updated:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if &amp;amp;location ne _ALL_ and %length(&amp;amp;locid_text)=0
   %then %do;
  update sas_gbl_input_parm
    set htl_cd = "&amp;amp;location"
     , parm_val = "&amp;amp;value"
     , current_ind = 'Y'
     , lst_updt_usr_id = "User_1"
     , lst_updt_ts = datetime()  
  where parm_nm = "&amp;amp;parm_nm"  
  ;
  %if &amp;amp;sqlobs=0 %then %do; /* no records updated */
  insert into sas_gbl_input_parm
     set parm_nm = "&amp;amp;parm_nm" 
     , htl_cd = "&amp;amp;location"
     , parm_val = "&amp;amp;value"
     , current_ind = 'Y'
     , lst_updt_usr_id = "User_1"
     , lst_updt_ts = datetime()  
  ;
  %end;
%end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 19 Jan 2018 08:35:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Insert-a-unique-record-within-macro/m-p/429048#M68535</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-01-19T08:35:21Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a unique record within macro</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Insert-a-unique-record-within-macro/m-p/429219#M68556</link>
      <description>&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jan 2018 18:07:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Insert-a-unique-record-within-macro/m-p/429219#M68556</guid>
      <dc:creator>Crubal</dc:creator>
      <dc:date>2018-01-19T18:07:04Z</dc:date>
    </item>
  </channel>
</rss>

