<?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: IF _N_=1 Then SET in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/IF-N-1-Then-SET/m-p/962260#M375061</link>
    <description>&lt;P&gt;You can use if "first.type then set type;" per&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;'s suggestion.&amp;nbsp; That would be the most similar to your other code solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or you can use the MERGE statement, as in:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=sashelp.cars out=cars;
  by type;
run;

data want;
  merge cars tbl1;
  by type;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This many-to-one merge process is probably the most common use of the MERGE statement (accompanied by the BY statement).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you don't like the idea of being required to sort sashelp.cars by TYPE, then you could load dataset TBL1 into a hash lookup table:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set sashelp.cars tbl1 (obs=0);
  if _n_=1 then do;
    declare hash h (dataset:'tbl1');
      h.definekey('type');
      h.definedata(all:'Y');
      h.definedone();
  end;
  if h.find()^=0 then call missing(avg_invoice);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 19 Mar 2025 17:21:58 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2025-03-19T17:21:58Z</dc:date>
    <item>
      <title>IF _N_=1 Then SET</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-N-1-Then-SET/m-p/962175#M375042</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I want to add values from one data set into another data set.&lt;/P&gt;
&lt;P&gt;I want to use IF _N_=1 Then set&amp;nbsp; statement.&lt;/P&gt;
&lt;P&gt;Example1 is working 100%&lt;/P&gt;
&lt;P&gt;In Example2 I dont get desired results.&lt;/P&gt;
&lt;P&gt;IS there a way to get the desired results in Example2 Via&amp;nbsp; IF then set statement?&lt;/P&gt;
&lt;P&gt;I want to add average values by type&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/**ex1**/
proc sql;
create table cars_a as
select mean(invoice) as AVG_Invoice
from sashelp.cars
;
quit;

Data Want;
set sashelp.cars;
If _N_=1 then set cars_a;
Run;

/**ex2**/
proc sql;
create table tbl1 as
select Type,
      mean(invoice) as AVG_Invoice
from sashelp.cars
group by Type
order by Type
;
quit;

proc sort data=sashelp.cars out=cars;by type;Run;
Data Want;
set cars;
by Type;
If _N_=1 then set tbl1;
Run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Mar 2025 05:33:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-N-1-Then-SET/m-p/962175#M375042</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2025-03-19T05:33:25Z</dc:date>
    </item>
    <item>
      <title>Re: IF _N_=1 Then SET</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-N-1-Then-SET/m-p/962178#M375045</link>
      <description>&lt;P&gt;Test first.TYPE instead of _N_&lt;/P&gt;</description>
      <pubDate>Wed, 19 Mar 2025 05:50:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-N-1-Then-SET/m-p/962178#M375045</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2025-03-19T05:50:13Z</dc:date>
    </item>
    <item>
      <title>Re: IF _N_=1 Then SET</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-N-1-Then-SET/m-p/962260#M375061</link>
      <description>&lt;P&gt;You can use if "first.type then set type;" per&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;'s suggestion.&amp;nbsp; That would be the most similar to your other code solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or you can use the MERGE statement, as in:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=sashelp.cars out=cars;
  by type;
run;

data want;
  merge cars tbl1;
  by type;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This many-to-one merge process is probably the most common use of the MERGE statement (accompanied by the BY statement).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you don't like the idea of being required to sort sashelp.cars by TYPE, then you could load dataset TBL1 into a hash lookup table:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set sashelp.cars tbl1 (obs=0);
  if _n_=1 then do;
    declare hash h (dataset:'tbl1');
      h.definekey('type');
      h.definedata(all:'Y');
      h.definedone();
  end;
  if h.find()^=0 then call missing(avg_invoice);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Mar 2025 17:21:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-N-1-Then-SET/m-p/962260#M375061</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2025-03-19T17:21:58Z</dc:date>
    </item>
    <item>
      <title>Re: IF _N_=1 Then SET</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-N-1-Then-SET/m-p/962263#M375064</link>
      <description>&lt;P&gt;If you want to do this with SQL (as in your Ex. #2), you can do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table tbl1 as
select a.*, b.avg_invoice
from
  sashelp.cars A
  inner join
  (select type, mean(invoice) as avg_invoice from sashelp.cars group by type) B
  on a.type=b.type;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Mar 2025 17:47:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-N-1-Then-SET/m-p/962263#M375064</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-03-19T17:47:55Z</dc:date>
    </item>
    <item>
      <title>Re: IF _N_=1 Then SET</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-N-1-Then-SET/m-p/962272#M375069</link>
      <description>&lt;P&gt;If you have a BY variable then you don't need to use that trick with the conditionally executed SET statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just use MERGE.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge cars tbl1;
  by type;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Mar 2025 18:38:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-N-1-Then-SET/m-p/962272#M375069</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-03-19T18:38:10Z</dc:date>
    </item>
  </channel>
</rss>

