SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
thryce85
Fluorite | Level 6

Hows it going?  noob sas sql question here . Im trying to learn foreign references and what Im trying to do is establish a primary foreign relationship and then delete from the primary and have it auto delete in the table with an associated foreign key .  Say I have stopped selling cars > 30000 because no one is buying them.  I want to delete all the cars in my records  that match this condition from all linked tables by simply deleting the entries in the table with the primary key.  I thought that using DELETE CASCADE would accomplish this but I must be missing sth.  It says this is not supported or maybe my syntax is wrong?  Any help would be appreciated.

 

%macro make_sql;

proc sql ;
create table k1 (id num , model char , msrp num ) ;
insert into k1
select monotonic()*2 as id , model , msrp from sashelp.cars;


quit;

 

proc sql;
alter table k1
add constraint prime primary key(id);
run;

proc sql ;
create table k2 (id num , model char, inventory num );
insert into k2
select monotonic()*2 ,model, invoice
from sashelp.cars;
quit;

proc sql ;
alter table k2
add constraint foreign2 foreign key(id)
references k1
on delete cascade ;
quit;

%mend make_sql;

 


%macro del_sql;
proc sql;
alter table k2
drop foreign key foreign2;

drop table k1;
quit;

proc sql;
drop table k1;
quit;
%mend del_sql;

 

%make_sql;

 

* condition to remove the primary values 
proc sql;
delete from k1
where msrp > 30000
;
quit;

 

*K1 deletes the values k2 stays the same? ;

proc contents data=k1 ;
run;

proc contents data=k2;
run;

 

*********************************************************

Ive also replaced ON DELETE CASCADE with ON UPDATE CASCADE 

then  used below.  its not working either so I must be missing sth very basic. 😞

 

proc sql;
update k1
set id = 0
where msrp > 30000
;
quit;

 

 

1 REPLY 1
SASKiwi
PROC Star

Please post your SAS log. It looks like you are using syntax which isn't supported in SAS SQL and the errors in the SAS log should point to these.

 

Not sure why you are experimenting with table keys and constraints. Please bear in mind that SAS data libraries are not RDBMSs like Oracle or SQL Server. There is no need to define table schemas before populating the tables - just populate the table and the table "schema" will take care of itself.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 1 reply
  • 765 views
  • 1 like
  • 2 in conversation