Write and run SAS programs in your web browser

problem with union in proc sql for appending two diff data

Reply
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....

 

CODE :-

 

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

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;
run;
proc append base=imp data=max_imp force;
run;

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;
    output;
    aon=max_aon;
    product_id=cat(product_id," ","Max");
    output;
  end;
  else output;
run;
Ask a Question
Discussion stats
  • 1 reply
  • 79 views
  • 0 likes
  • 2 in conversation