Label for a compare (except query) in SAS - sas

hi I'm running the below.
1st query comes back with what I need... I just wanted to add a column to know the inviroment of the object. the 2nd query doesn't work cuz its looking at the 'P' and 'T' column and comparing that
1)
proc sql;
create table ALL_compare_PROD_TEST as
(select * from PROD Except select * from TEST)
UNION ALL
(select * from TEST Except select * from PROD)
ORDER BY code ASC
;QUIT;
2)
proc sql;
create table ALL_compare_PROD_TEST as
(select 'P'as PROD, * from PROD Except select * from TEST)
UNION ALL
(select 'T'as TEST, * from TEST Except select * from PROD)
ORDER BY code ASC
;QUIT;

Sounds like you just need another level of subquery. Also you will want the P and T to be in the same variable.
proc sql;
create table ALL_compare_PROD_TEST as
select 'P' as SRC,* from (select * from PROD except select * from TEST)
UNION ALL
select 'T' as SRC,* from (select * from TEST except select * from PROD)
ORDER BY code ASC
;
quit;

Related

what is wrong with my query in oracle apex?

The following code does not work, but it does work when I convert it to the next code. Why?
select * ,count(*) over(partition by colour) as counts_by_colour
from bricks;
output:
ORA-00923: FROM keyword not found where expected
modified:
select b.* ,count(*) over(partition by colour) as counts_by_colour
from bricks b;
This is just the way SQL works - nothing to do with APEX.
select * means select all columns from what follows. So...
select * from emp join dept;
...returns all columns from emp and all columns from dept.
You are not allowed to select anything else with select * - e.g. ...
select *, 'abc' from emp;
raises the same error you got.
However, you can use select alias.* to select all columns from one table/view in the query, and then you are also allowed to select other things:
select e.*, 'abc' from emp e;
or
select e.*, d.loc from emp e join dept d;
The "implicit" alias also works:
select emp.*, 'abc' from emp;

Sql to proc sql conversion

I have 2 tables that contains var values like (ID, date, var1,var2,var3....)
I need to get the data from table2 and add it to table1 for which (ID or date) does not exist in table1.
I am using the below code in sql to get new ID's from tab2 to tab1:
INSERT INTO table1
SELECT * FROM table2 a
WHERE ID not in(select ID from table1 where ID=a.ID)
Here is the code to add new date for existing ID's in tab2 to tab1 :
INSERT INTO table1
SELECT * FROM table2 a
WHERE date not in(select date from table1 where ID=a.ID)
I don't know how to do this in proc sql.
Please share an effective method to do this task.
To insert the new ID I used:
proc sql;
create table lookup as
select a.ID
from table1 a inner join table2 b
on a.ID = b.ID
;
insert into table1
select * from table2 a
where a.ID not in (select ID from lookup)
;
quit;
This works well. But, it failed to insert a date for existing IDs.
Please suggest some ideas to complete this step.
Thanks in advance!
SAS SQL is Similar to the SQl you have written.
The SAME insert statements can be warped as proc sqls in SAS and they work like charm.
If your SQL did work then the following will work too.
PROC SQL;
INSERT INTO work.table1
SELECT * FROM work.table2 a
WHERE ID not in(select ID from work.table1 where ID=a.ID);
INSERT INTO work.table1
SELECT * FROM work.table2 a
WHERE date not in(select date from work.table1 where ID=a.ID)
QUIT;

Joining two sets in SAS SQL with table/correlation error

I am trying to join two datasets. The first dataset1 has two columns item and price. The second dataset2 has three columns - item, customerid, and qty. I need to only include the unique rows from dataset1 that are not in dataset2. While trying to implement this code, I get the error:
Error: Unresolved reference to table/correlation name i.
I am unsure how to fix this error, thanks.
PROC SQL;
create table a as
select *
from dataset1 as i
except corr
select *
from dataset2 as p
where i.item = p.item;
describe table a;
QUIT;
EXCEPT is used to select records in the first set that do not exist in the second set. So if what you want is, to quote you, select records from dataset1 that do not appear in dataset2, you don't need the where clause:
PROC SQL;
create table a as
select *
from dataset1 as i
except corr
select *
from dataset2 as p
;
QUIT;
If however, like that where clause would suggest, you actually want to select records from dataset1 where the value of item is not found in dataset2, you could do this
proc sql;
select *
from dataset1 i
where not exists (select *
from dataset2 p
where i.item=p.item
)
;
quit;
EDIT: following your latest comment, and if you reaaaally need your query to feature an except, this should get you your result
proc sql;
create table a as
select t1.*
from dataset1 t1
inner join (select *
from dataset1 as i
except corr
select *
from dataset2 as p
) t2
on t1.item=t2.item
;
quit;
Even though this will do the same as the query above (with not exists) or, now that I think of it (stupid me), as this:
proc sql;
create table a as
select *
from dataset1
where item not in (select distinct item from dataset2)
;
quit;

Concatenate two SAS data sets, but only if ID appear in both

I want to concatenate two SAS data sets, one from 2003 and one from 2013. There is a uniq identifier in both, and I'll only allow allow records to be concatenated if they appears in both.
NB. there is multiple records with the same ID.
Here's some untested code:
proc sql;
create table want as
select * from(
select * from t1 where t1.id in (select t2.id in t2)
union
select * from t2 where t2.id in (select t1.id in t1)) as A;
quit;

How do you re use a field in SAS in proc sql?

If have the data step:
data myRegions;
set myRegions;
ext_price = price * qty;
mix = weighted_calc * ext_price;
run;
I wan to do this on SQL as I want to use some groupings and subqueries
but do I hace t make price * qty operation everytime I want to use that value?!
You can use calculated , from the docs:
CALCULATED enables you to use the results of an expression in the same
SELECT clause or in the WHERE clause. It is valid only when used to
refer to columns that are calculated in the immediate query
expression.
Here is an example:
proc sql;
create table myRegions as
Select a.*,
(price * qty) as ext_price ,
(weighted_calc * calculated ext_price ) as mix
from myRegions;
quit;