Write and run SAS programs in your web browser

problem with union in proc sql for appending two diff data

Occasional Contributor
Posts: 19

problem with union in proc sql for appending two diff data




i have a data where AON is having multiple entries. i fond entries which have MAX(AON) and created a table.

then i wish to append the new and orriginal file but hard luck.

anyhelp guys....




proc sql;
create table ans_4B as select* from work.import
group by PRODUCT_ID
select PRODUCT_ID , MAX_AON from work.ans_4A;

Super User
Super User
Posts: 7,392

Re: problem with union in proc sql for appending two diff data

Please elaborate on what does not work, provide test data in the form of a datastep in the post (use the {i} to preserve formatting) and what the output should look like.


At a guess from the minimal info you provided:

Union appends one dataset to another asumming that both have the same variables.  This is presumably not the case as you select * from the first, and select 2 variables in the second which is unlikely to be the same.  Also, you do not group by like, it wouldn't do anything to the data, but is pretty much invalid in that sense.


If you want just to add the largest:

proc sort data=imp out=max_imp nodupkey;
  by product_id descending aon;
proc append base=imp data=max_imp force;

Or you could just do it in one datastep, retain the aon each row by product_id and at the end of the group output twice:

data want;
  set imp;
  by product_id;
  retain max_aon;
  if first.product_id then max_aon=aon;
  else if aon > max_aon then max_aon=aon;
  if last.product_id then do;
    product_id=cat(product_id," ","Max");
  else output;
Ask a Question
Discussion stats
  • 1 reply
  • 2 in conversation