BookmarkSubscribeRSS Feed
Pradeepbanu
Obsidian | Level 7

I want to store count of dataset in variable like below

%let Cnt ;

 

create table work.delaycheck as
select * from connection to oracle
(

SELECT PTNR_ID,CLNT_ID,REPORTING_DATE_KEY,NET_SALES
FROM FACT_TABLE

MINUS

SELECT PTNR_ID,CLNT_ID,REPORTING_DATE_KEY,NET_SALES
FROM HIST_FCT

);

 

I want to store count of this table in the variable Cnt like below

 

Set Cnt = (Select count(*) from work.delaycheck )

And Then

If(Cnt=0)

THEN

DO NOTHING

ELSE

execute(

Insert into Oracle_table

select * from work.delaycheck

 

) by oracle;

disconnect from oracle;

quit;

 

How can I acheive these steps? Thanks In advance!!

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Why?  Every dataset within the SAS system already has metadata associated with the dataset stored.  You can access this metadata directly by using the sashelp.vtable (and if you want columns, sashelp.vcolumn) or via the SQL syntax of dictionary.tables.  Therefore you a) do not need to code this yourself, and b) do not need to store the numeric in a text macro variable.  So for instance:

set cnt=(select nobs from sashelp.vtable where libname="WORK" and memname="ABC");

Assuming your dataset is work.abc.

s_lassen
Meteorite | Level 14

Why do you want to make the insert conditional? When the table is empty, inserting in into another table does exactly nothing, and takes very little time.

 

But if you need to know this for other reasons, PROC SQL assigns the macro variable SQLOBS, which contains the number of rows in the output from the last SQL statement. So you could use that like this:

%macro insert;
proc SQL;
  /* code to create work table here */
%if &sqlobs>0 %then %do;
  /* code to do something with table here */
  %end;
quit;
%mend;

%insert;  
  

Of course, you cannot use a SQL EXECUTE call to insert rows from a SAS table. You would either have to load the data to a (temporary) Oracle table, and the perform the insert. Alternatively, you can assign the Oracle schema as a SAS libname and use e.g. PROC APPEND to insert your data:

proc SQL;
  connect to Oracle(<connnect options>);
  create table work.delaycheck as select * from connection to Oracle
    (<Oracle select code>);
quit;

libname outlib Oracle <connect options>;
proc append data=delaycheck base=outlib.Oracle_table;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 3670 views
  • 2 likes
  • 3 in conversation