Blog

  • Recursive queries in Teradata

    Similarly to Oracle connect by level (see also https://archief.van-maanen.com/index.php/2016/04/08/an-oracle-programme-with-a-loop/ ), Teradata has a structure whereby tables can be connect in order to create a potentially large table. Let us show some code:

    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.

    The query provides the answer.


  • Using SAS Webreporting

    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: information on table

    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.

  • Deploy jobs in SAS

    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:

    "C:\Program Files\SASHome\SASFoundation\9.4\sas.exe" -sysin C:\Users\tomva\SynologyDrive\SAS\Hello.sas -log C:\Users\tomva\SynologyDrive\SAS\Hello.log

    I have scheduled this task in the Windows Task scheduler. This task then looks like:

  • Like any

    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%’).

  • SAS, error but correct outcomes

    Recently I noticed that an error in SAS does not automatically lead to wroung outcomes. Look what happens.

    I created two tables that are sorted by a field (nummer).

    proc import datafile = 'C:\Users\tomva\SynologyDrive\SAS\input.xlsx'
    dbms=xlsx out=input replace;
    run;
    proc means data=input sum noprint;
    by nummer;
    var aantal;
    output out=sumtot sum=;
    run;
    data sumtot(drop=_type_ _freq_);
    set sumtot(rename=(aantal=sum_aantal));
    run;
    proc sort data=sumtot;by nummer;run;
    proc sort data=input;by nummer;run;

    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:

    nummer=1 aantal=15 teller=2 sum_aantal=25 aandeel=0.6 ERROR=1 IORC=1230015 N=2
    nummer=2 aantal=13 teller=4 sum_aantal=43 aandeel=0.3023255814 ERROR=1 IORC=1230015 N=4
    nummer=2 aantal=18 teller=5 sum_aantal=43 aandeel=0.4186046512 ERROR=1 IORC=1230015 N=5

    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.

  • Adding data to a SAS dataset

    This short article describes how to add two files with data to a SAS dataset. Assume both files use the same format for the data.

    The first approach uses a more or less standard technique wereby a global variable (fileref) is created that refers to two files.

    filename gegs ('C:\Users\tomva\SynologyDrive\SAS\gegs.txt' 'C:\Users\tomva\SynologyDrive\SAS\gegs2.txt');
    data ff;
    infile gegs;
    input nummer 1-3 naam $;
    run;
    

    The second approach creates a place holder that is linked to a file variable that is loaded with different file names. Each file is read sequentially. While the SAS read procedure loops through the records, each record is written to the target SAS dataset with the output statement.

    data score45;
    infile datalines;
    length rfile $60;
    input rfile $;
    infile PH filevar=rfile end=lastobs;
    do until (lastobs);
    input nummer naam $;
    output;
    end;
    datalines;
    C:\Users\tomva\SynologyDrive\SAS\gegs.txt
    C:\Users\tomva\SynologyDrive\SAS\gegs2.txt
    ;
    run;
  • Exporting data from Teradata

    Recently, I came across the so-called fast export facility in Teradata. This facility allows you to export data from Teradata in a relative fast way. Let us look at the script:

    .LOGTABLE tom.invoer_log;  
     .logon 192.168.178.13/tom,******;
     database tom;
     .BEGIN EXPORT SESSIONS 2;  
           .EXPORT OUTFILE C:\Users\tomva\Documents\invoer.txt  
           MODE RECORD FORMAT TEXT;
           SELECT CAST(leeftijd AS CHAR(10)), 
              CAST(nummer AS CHAR(10))   
           FROM
           invoer;
        .END EXPORT;
     .logoff;
    

    The script can be evoked by “fexp < t.txt”, t.txt being the name of the script. The output then looks like:

  • Teradata BTEQ

    Teradata is a DBMS that is more or less created to handle massive amounts of data. I now work with it and this DBMS is able to handle 500 million rows in a split second. Brilliant. Teradata has a tool BTEQ that can be used to run scripts. Let us show a very simple script:

    .logon 192.168.178.13/tom,*********
     database tom;
     select * from invoer;
     .logoff

    This script can be run with the command “C:\Program Files (x86)\Teradata\Client\15.00\bin>bteq<C:\Users\tomva\Documents\s.txt”

    The output looks as:

  • SVN version controller

    Recently, I came accross the SVN version control mechanism. The idea is that we have a so-called working directory that acts as a source for a final version of some documents. Once we have a final version, the final version can be committed to another platform. This other platform can be accessed by others.

    If we have a revised version, it can be subsequently committed. This revised version can then be accessed by others to want to have the revised version.

    Let us look at the working directory. This looks like:

    The .svn folder indicates that we have a working directory that contains files that may act as a final version of a document. Let us assume for the moment that “test.txt” may be used as a document that can be committed as a final version.

    On the other hand, let us look at the platform where others may find the final version:

    The platform is a website where documents can be accessed as final versions. We may download file ” test.txt” to see the latest committed version. We also see that we have version “r2” as the latest version.

    How do we commit a next version as a final version?

    The first command is “svn update”, whereby it is indicated that test.txt is changed in the working directory.

    The second command is “svn commit -m “1234 toegevoegd””

    We then see that the file is updated.

    A new version “r3” is then shown.