A nice feature of a DBMS is to be able to calculate elapse period. Important questions like “how many days to go to pension?’, can be addressed. In teradata, this looks like:
select cast(1231125 as date) - current_date;
This little sentence involves a typecasting from an integer to a date. The integer is 1221125 that stands for 1900+123 = 2023 as the year in which pension will start, 11 as the month and 25 as the day. So cast(1231125) leads to 25 November, 2023. The current date is now. The difference is the number of days before retirement. In this case 766 days.
Teradata recently introduced the concept of pivoting in its sql. The idea is relatively straightforward. A column is selected that has some distinct values. Pivoting then implies that the distinct values are translated into columns. Doing so another value is used to derive the actual content in the columns. This can be a sum, maximum or any other aggregate.
Let us start with a table. Its content is:
Let us use yr as the column that is pivoted. It has 2010 and 2011 as distinct values. We will have “2010” and “2011” as new columns. These columns are loaded with aggregates that are derived from another column, sales.
Let us look at the code:
SELECT "2010" as sales_2010, "2011" as sales_2011 FROM (select yr, sales from s1)s1 PIVOT (SUM(sales) FOR yr IN (2010,2011)) tmp;
Notice that we use an inline query as the source. This inline query only contains yr and sales. Column yr is translated into two columns: 2010 and 2011. The end result will be one row with two two columns. The values in the row will be an aggregate (=sum) on sales.
So, we get:
We can also retain an extra column from the original table. Let us retain “mon”. The code then gets:
SELECT mon, "2010" as sales_2010, "2011" as sales_2011
FROM (select yr, mon, sales from s1)s1
PIVOT (SUM(sales) FOR yr IN (2010,2011)) tmp;
Today, I played with the new SAS Viya environment. This environment has been promoted as a cloud based environment by SAS that acts as a SAAS solution to analytics needs from an organisation. This implies that we only need an internet connection and a browser to access this environment.
How does it work. A small example might show you the working.
We may upload an Excel file to this environment with:
FILENAME REFFILE FILESRVC FOLDERPATH='/Users/tom.van.maanen@capgemini.com/My Folder' FILENAME='invoer.xlsx'; PROC IMPORT DATAFILE=REFFILE DBMS=XLSX OUT=WORK.NAMEN; GETNAMES=YES; RUN; data test; set namen; run;
This imports a file (= invoer.xlsx) into the traditional SAS work library. The file was imported from a local computer into the cloud environment. This cloud environment is made visible in the Viya Cloud environment with:
We then uploaded this file into work.namen. It can be seen in the library:
We then start a CAS session. This stands for Cloud Analytic Service. Next code takes care of this. Subsequently, I assign the CAS session as a library.
cas mysession sessopts=(caslib=casuser timeout=1000 locale="en_US");
caslib _all_ list;
caslib _all_ assign;
proc casutil;
list tables;
run;
The result is that I can see libraries that are connected to the CAS session:
I then copy the work.names dataset into a CAS library with:
I found one possibility to gracefully end a SAS job. The code is rather straight forward. One opens a macro with a condition statement %if %then etc. If the condition is fulfilled, the code may run as previously anticipated. If not, a return statement ends processing the procedure.
with
RECURSIVE jaren(belastingjaar) as
(
select belastingjaar
from (select 2015 as belastingjaar)A
union all
select belastingjaar+1
from jaren
where belastingjaar < 2020
)
select belastingjaar from jaren;
This generates a series going from 2015 to 2020.
Or, in another example:
with
RECURSIVE trap(employee_id, naam) as
(
select employee_id , naam
from tom.organisatie
where employee_id =1
union
all
select organisatie.employee_id, organisatie.naam
from tom.organisatie
inner join trap
on trap.employee_id = organisatie.manager_id
)
select employee_id,naam
from trap;
, which is based on this table
The purpose of the query is to provide an overview of all members who have have employee_id = 1 as their direct or indirect manager. From the table, we see that employee_id = 2,3 have employee_id=1 as their direct manager. However number 4,5 have number 2 as their direct manager and they are therefore indirectly managed by number 1.
SAS currently allows to create simple reports via a web portal. This works as follows. Assume, one has an environment where the correct installation is made. One may then invoke the web report studio via call from the browser. In my case. it is http://sasspeel:80/SASWebReportStudio.
The advantages are clear. One may share the report across the intranet. One may create reports without having a SAS license on the local desktop. In short: the usual advantages of a web enabled application over a desktop application.
Let me show the big steps.
The first big step is to make data available in Management Console. Later one reports on these data via Webreports.
The data are made available by registering the table in the Management Console. This can be seen as a technical description of a table. The description is stored in a location that can be accessed by other tools.
In another step, the technical description is used to create a logical description. For that purpose the so-called Information Map is used. This allows a translation from technical tables & fields into entities with categories and measurements. SAS calls such a logical description an Information Map Contents.
In a final step, one may invoke the Web Report Studio. This can be used to create a report (via a Wizard) or to view exiting report. Using the Wizard, I created next report:
Teradata has a nice command to get a quick overview on tables. This command is as follows: help statistics table_name.
In the output, I see indicators like the data and time of the creation, the number of unique rows etc.
This command can be combined with another command like: select * from dbc.tables where databasename = ‘database’ . This provides an overview on who created the table, what primary key is used.
One may use the SAS Management Console to deploy SAS jobs. The trick is as follows. One must have a SAS job that could be either created from the SAS programme or could be exported from Enterprise Guide. Let me provide an example of such a programme:
This can be deployed, using in SAS Management Console, option Schedule Manager, right mouse click deploy SAS Datastep programmme. This then provides a possibility to deploy a programme.
It stores the code in a certain location. In my case, this is: C:\SAS\DIserver\Lev1\SASApp\SASEnvironment\SASCode\Jobs .
It also makes the code available as a deployed job. This can then be used as a step in a workflow. This workflow is subsequently listed in the schedule manager.
Ideally, it could the be run from within the schedule manager. An alternative is to create a batch file that can be run as any other programme from a central schuler. This batch programme may look like:
Today, I learned something that could be quite handy. Quite often I wanted the like condition on two conditions. I then created a SQL statement like: select * from XXX where field like ‘%ni%’ or field like ‘%NI%’. This can done in a less complicated formula. This looks like select * from XXX where field like any (‘%ni%,’%NI%’).
I then decided to join the tables on [nummer], the very field that is used to sort the records upon.
I did this in two steps.
proc datasets library=work noprint; modify sumtot; index create nummer; run;
data som; set input; set sumtot key=nummer; aandeel = aantal/sum_aantal; run;
The output looked as I expected. But then I saw in the logfile something strange: a warning saying that several records were not matched due to absence of data:
This can be explained by the fact that we have an n to 1 join, whereby in the first match a record from the n side is read along with a record from the 1 side. In the next round, another record from the n-side is read but no other record in the 1 side is found that matches the record. Therefore the old values are retained and subsequently written as the run statement is encountered.