Is it possible to get source code from SAS view table? - view

I have a functioning SAS view table, but the source code which creates this view is lost.
Is it possible to extract it directly from the view file itself?

Many views in SAS are created with PROC SQL. Datastep view are very uncommon. So DESCRIBE VIEW might work.
proc sql;
describe view VIEWNAME;
quit;
NOTE: the view code will be written to log

You could also try:
data view = myview;
describe;
run;
This is more likely to work if you run it on the same system where myview was originally created.

It's not pretty but you could read the definition from the binary view file itself.
See the following example:
data myview /view=myview;
set sashelp.class;
x=age;
height=age*x;
run;
data _null_;
infile "%sysfunc(pathname(work))/myview.sas7bvew";
input;
put _infile_;
run;
Which gives the below in the log:

DESCRIBE;
Without Arguments
Use the DESCRIBE statement to retrieve program source code from a stored compiled DATA step program or a DATA step view. SAS writes the source statements to the SAS log.
25 data v / view=v;
26 set sashelp.class;
27 if sex eq 'F';
28 run;
NOTE: DATA STEP view saved on file WORK.V.
NOTE: A stored DATA STEP view cannot run under a different operating system.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
29
30 data view=v;
31 describe;
32 run;
NOTE: DATA step view WORK.V is defined as:
data v / view=v;
set sashelp.class;
if sex eq 'F';
run;

Related

Impossible to delete table in SAS Enterprise Guide

I can't process to deleting my table. I get this error:
"ERROR: File SASUSER.MCO.DATA is not a SAS data set."
I've tried many ways to delete but neither works.
Thanks for felp!
tested with (proc delete / proc sql drop / %deltable )
I have used the bellow codes:
proc sql; drop table sasuser.MCO; quit;
%deltable (tables=sasuser.MCO)
proc datasets nolist lib=sasuser; delete MCO ; quit;
log and result of proc datasets lib=sasuser; run;
this is the log:
here
and the result is here
From your error message it seems that the file is NOT an actual SAS dataset. I have never seen a SAS dataset on Unix that as only one thousand bytes long, even an empty dataset is normally more like 14K, depending on the default block size that SAS uses to create the files.
So just use the operating system to delete the file. The name of the file should be mco.sas7bdat and it should be in the directory that the SASUSER libref is pointing to. So if you have XCMD option active you could just use code like this:
x "rm %sysfunc(pathname(sasuser))/mco.sas7bdat";
If XCMD is not active then you will need to use the FDELETE() function instead.

Cannot read SAS View using different libname other than what was used to create the view

I created a view in SAS to read multiple dataset. I can read data from this view only if I give same libname that was used to create the view.
libname test "c:\temp";
data test.one; x=1; run;
data test.two; x=2; run;
data test.three; x=3;
run;proc sql ;
create view test.master as
select * from test.one union
select * from test.two union
select * from test.three;
quit;
data test;
set test.master;
run;
The above code runs fine but when I open a new sas session and use different
libname as below I get error:
libname new "c:\temp";
data test;
set new.master;
run;
ERROR: Libref TEST is not assigned.
ERROR: Libref TEST is not assigned.
ERROR: Libref TEST is not assigned.
ERROR: SQL View TEST.PANEL2 could not be processed because at least one of the data sets, or views, referenced directly (or
indirectly) by it could not be located, or opened successfully.
Please Advise
I am not sure that I think that is good idea, but if you do want to do that then you could embed the libname definition into the view with the using clause. Put the path into the view name also and you don't even ever have to create the libref test at all.
proc sql ;
create view 'C:\temp\master' as
select * from test.one union
select * from test.two union
select * from test.three
using libname test 'C:\temp'
;
quit;
Now you are not required to use the same libref, but note that you are required to use the same physical location.
Create View
A create view creates the instructions to use when the view is accessed for processing. The select statement is checked for syntax, but not checked regarding data sources. Thus, the view construct contains the unverified librefs, table names, and the column names it will need during processing.
You can create a view even when the session has no active libnames (see Example below). View creation is akin to writing and saving a SAS program, but not submitting it right away.
Libname and libref
The libname statement creates a library reference, or libref for short. From documentation (my bold):
LIBNAME Statement
Associates or disassociates a SAS library with a libref (a shortcut name), clears one or all librefs, lists the characteristics of a SAS library, concatenates SAS libraries, or concatenates SAS catalogs.
The term libname is used in discussion when identifying an instance of a libref, but sometimes the libname is used when the more technically precise term libref is meant. The typical experience for SAS users is that a libname associates to a single folder or directory.
View use (execution)
When the view is accessed the processing instructions are executed, using the current state of the SAS session, with respect to the active librefs, in order to create the result set.
This is a good thing!
In an ad-hoc environment a view can be passed amongst users and they can decide which library the libref points to.
In a production environment the SAS admin would ensure the libref points to the proper and current library.
In order to use a view in the same session state it was when it was created, the current and future sessions must have the same librefs and necessary library contents.
If a view fails for libnames reasons, simply create the needed libnames pointing to the proper libraries.
Example
Richard the view maker
libname shazaam; * now there is no shazaam ! ;
proc sql;
create view sasuser.lightning as
select * from shazaam.powersource
where situation='danger'
and pigs='fly'
and
;
Richard sends his sasuser.lightning view (*.sas7bvew) to Bob and Jane.
Bob's session
libname shazaam 'c:\super-heroes\shaz\sasdata';
proc sql;
select * from sasuser.lightning;
--- FAILS because shazaam.powersource exists but powersource does not have column `pigs`.
Jane's session
libname shazaam 'c:\heroes\shazaam_v2\sasdata'
proc sql;
select * from sasuser.lightning;
--- WORKS because in Jane's '_v2' data shazaam.powersource exists and column `pigs` is present
The problem is the 'test' in the view query, not the libname. Remember, the view is only run when you try and access it.
So if the library exists the view can run, but since the test library doesn't exist, it's trying to read from library that doesn't exist.
Here's an example of your 'view' that does work:
libname test "/folders/myfolders/Temp";
proc sql ;
create view test.master as
select * from sashelp.cars (obs=4) union
select * from sashelp.cars (firstobs=10 obs=14) union
select * from sashelp.cars (firstobs=30 obs=34);
quit;
data test;
set test.master;
run;
libname test ;
libname new "/folders/myfolders/Temp";
data test2;
set new.master;
run;
#Tom has provided a workaround.

SAS: sort error (by variable not sorted properly)

This question is a follow up from another I had here SAS: Data step view -> error: by variable not sorted properly; I am opening a new question as the desired solution is slightly different: As I am looping through several input files, one of the raw-files is not properly sorted, I wonder what I could do to make my program to skip that particular input file and just continue?
Quote:
I am using a macro to loop through files based on names and extract data which works fine for the majority of the cases, however from time to time I experience
ERROR: BY variables are not properly sorted on data set CQ.CQM_20141113.
where CQM_20141113 is the file I am extracting data from. In fact my macro loops through CQ.CQM_2014: and it works up until 20141113. Because of this single failure the file is then not created.
I am using a data step view to "initialize" the data and then in a further step to call data step view (code sample with shortened where conditions):
%let taq_ds = cq.cqm_2014:;
data _v_&tables / view=_v_&tables;
set &taq_ds;
by sym_root date time_m; *<= added by statement
format sym_root date time_m;
where sym_root = &stock;
run;
data xtemp2_&stockfiname (keep = sym_root year date iprice);
retain sym_root year date iprice;
set _v_&tables;
by sym_root date time_m;
/* some conditions */
run;
When I see the error via the log file and I run the file again, then it works (sometimes I need a few trials).
I was thinking of a proc sort, but how to do that when using data step view?
Please note the cqm-files are very large (which could also be the root of the problem).
End Quote:
edit: I tried your code (and deleted the by statement in the data step view), however I am getting this error:
NOTE: Line generated by the macro variable "TAQ_DS".
152 cq.cqm_2013:
_
22
200
ERROR 22-322: Syntax error, expecting one of the following: a name, ;, (, ',',
ANSIMISS, AS, CROSS, EXCEPT, FULL, GROUP, HAVING, INNER,
INTERSECT, JOIN, LEFT, NATURAL, NOMISS, ORDER, OUTER, RIGHT,
UNION, USING, WHERE.
ERROR 200-322: The symbol is not recognized and will be ignored.
NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of
statements.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE SQL used (Total process time):
real time 0.02 seconds
cpu time 0.00 seconds
ERROR: File WORK._V_CQM_2013.DATA does not exist.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: SAS set option OBS=0 and will continue to check statements.
This might cause NOTE: No observations in data set.
WARNING: The data set WORK.XTEMP2_OXY may be incomplete. When this step was
stopped there were 0 observations and 8 variables.
NOTE: DATA statement used (Total process time):
real time 0.02 seconds
cpu time 0.00 seconds
Do you need the by statement in the view creation?
If not, then sort from the view into a temporary data set:
proc sort data=_v_&tables out=__temp;
by sym_root date time_m;
run;
data xtemp2_&stockfiname (keep = sym_root year date iprice);
retain sym_root year date iprice;
set __temp;
by sym_root date time_m;
/* some conditions */
run;
Another option would be to create the view in PROC SQL adding the sort order:
proc sql noprint;
create view _v_&tables as
select <whatever>
from &taq_ds
where <clause>
order by sym_root, date, time_m;
quit;

SAS: Data step view -> error: by variable not sorted properly

I am using a macro to loop through files based on names and extract data which works fine for the majority of the cases, however from time to time I experience
ERROR: BY variables are not properly sorted on data set CQ.CQM_20141113.
where CQM_20141113 is the file I am extracting data from. In fact my macro loops through CQ.CQM_2014: and it works up until 20141113. Because of this single failure the file is then not created.
I am using a data step view to "initialize" the data and then in a further step to call data step view (code sample with shortened where conditions):
%let taq_ds = CQ.CQM_2014:;
data _v_&tables / view=_v_&tables;
set &taq_ds;
by sym_root date time_m; *<= added by statement
format sym_root date time_m;
where sym_root = &stock;
run;
data xtemp2_&stockfiname (keep = sym_root year date iprice);
retain sym_root year date iprice;
set _v_&tables;
by sym_root date time_m;
/* some conditions */
run;
When I see the error via the log file and I run the file again, then it works (sometimes I need a few trials).
I was thinking of a proc sort, but how to do that when using data step view?
Please note the cqm-files are very large (which could also be the root of the problem).
edit: taq_dsis not a single file but runs through several files whose name start with CQM_2014, i.e. CQM_20140101, CQM_20140102, etc.
Based on the code provided, you could replace your first data step view with a SQL one:
proc sql;
create view _v_&tables as
select * from &taq_ds
where sym_root = &stock
order by sym_root, date, time_m;
Alternatively you could prefix your data step view with a similar view. This would enforce the ordering needed for the subsequent by statement.
Creating an index on taq_ds corresponding to the by group order would also solve this, e.g.:
proc datasets lib=<library containing taq_ds>;
modify taq_ds;
index create index1=(sym_root date time_m);
run;
quit;

How to fetch data from Oracle cursor into a temporary table and then print out the data in SQLPlus?

I am fetching data from cursor in the way like this:
variable rc refcursor
spool file.txt
set timing on
exec :rc := someoraclepackage.somefunction(some,parameters,here)
set timing off
print rc
spool off
But I've identified a problem with TIMING. Some packages give me the real time of their execution, but some other not. They show me time like 120 miliseconds, and the real time of processing the package is for example 10 minutes.
I need the exact time of the package processing for performance testing.
All I have heard about the solution of this problem is:
Inserting the data from the cursor to a temporary table (database server side), to count the real package runtime.
Running a select * on the temporary table to fetch the data (jenkins side)
But I have no Idea where to start with putting the data from the cursor to a temporary table, and I don't know if this is the real solution of the problem...

Resources