BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Ronein
Onyx | Level 15

Hello

I want to add values from one data set into another data set.

I want to use IF _N_=1 Then set  statement.

Example1 is working 100%

In Example2 I dont get desired results.

IS there a way to get the desired results in Example2 Via  IF then set statement?

I want to add average values by type

/**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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

You can use if "first.type then set type;" per @ChrisNZ's suggestion.  That would be the most similar to your other code solution.

 

Or you can use the MERGE statement, as in:

proc sort data=sashelp.cars out=cars;
  by type;
run;

data want;
  merge cars tbl1;
  by type;
run;

This many-to-one merge process is probably the most common use of the MERGE statement (accompanied by the BY statement).

 

 

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:

 

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

4 REPLIES 4
ChrisNZ
Tourmaline | Level 20
mkeintz
PROC Star

You can use if "first.type then set type;" per @ChrisNZ's suggestion.  That would be the most similar to your other code solution.

 

Or you can use the MERGE statement, as in:

proc sort data=sashelp.cars out=cars;
  by type;
run;

data want;
  merge cars tbl1;
  by type;
run;

This many-to-one merge process is probably the most common use of the MERGE statement (accompanied by the BY statement).

 

 

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:

 

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
quickbluefish
Barite | Level 11

If you want to do this with SQL (as in your Ex. #2), you can do this:

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;
Tom
Super User Tom
Super User

If you have a BY variable then you don't need to use that trick with the conditionally executed SET statement.

 

Just use MERGE.

data want;
  merge cars tbl1;
  by type;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1308 views
  • 10 likes
  • 5 in conversation