Friday, March 21, 2014

SAS in Mainframes(z/Os) Tutorial with xamples

To know the SAS Basics, check  Chapter 1  . It demonstrates the very basic working principle of sas.
With your understanding of  the basics in sas, we will start the SAS in mainframe (Z/OS) environment .
The very first thing to know:  Turning Raw Data Into Information is what SAS is all about !!!!

This is the basic principle of how SAS works. The raw data(input file in JCL) is read into SAS through INFILE keyword.This has the same name as the DD name in the JCL.
Once the file is read, the next step is to structure/format the data in SAS Dataset through INPUT keyword as explained below.
 
DATA RYAN;
  INFILE POLIN ;
  INPUT @10  FNAME    $CHAR10.
              @24  ACCTNO   $CHAR05;


Here the keyword DATA  implies the starting of DATA step. RYAN is the name of the Data step.It can be any name.
INFILE POLIN; This is where the raw data is read by sas. POLIN is the JCL DD name for the input.
INPUT @ FNAME $CHAR10...; These statements structures the input dataset read above(here POLIN) and creates a dataset which is internal to SAS(commonly called the SAS data set). Internally SAS would be using this data structure created.
So here the SAS dataset contains FNAME (10 bytes) which is @ 10th position in input file and ACCTNO(5 bytes) which is @ 24th position in input.It wont consider the other records which might be present in the input dataset.
After that we manipulate the data as per our needs and do various functions.In all SAS programs only two steps are of utmost importance namely the DATA step and PROC step.

SAS Syntax Rules:  (A few very handy rules to make life simple!)Can begin and end in any column.
One or more blanks or special characters can be used to separate words.
A single statement can span multiple lines.
Several statements can be on the same line.
/* to begin a comment and */ to end it
 A SAS program generally Mostly comes with the default installation of Z/OS just like DFSORT.Only u need to know the library.
Lets start with few of the basic inbuilt functions and see the output in spool.No input file is required here.Lets see the DATE Function in SAS
//X15122RY  JOB (10,&SYSUID),'RYAN',CLASS=T,

//    MSGCLASS=V,NOTIFY=&SYSUID
//*                                   
//SAS01     EXEC SAS
//WORK      DD SPACE=(CYL,(50,10),RLSE)
//SYSIN     DD *                                            
OPTION NOCENTER;                                                  
OPTION SORTLIB='';
  DATA RYAN;       
    THISYEAR = YEAR(TODAY());           
    THISMONTH= MONTH(TODAY());          
    THISDAY  = DAY(TODAY());            
    LASTYEAR = YEAR(TODAY()) - 1;       
    DATE = TRIM(LEFT(THISYEAR))|| '1101';
  PROC PRINT DATA= RYAN;
   VAR THISYEAR LASTYEAR THISMONTH DATE THISDAY
//*

Output:
The SAS  System
Obs    THISYEAR    LASTYEAR    THISMONTH      DATE      THISDAY
 1       2013        2012          6        20131101       20   

Looks Cool!
Now , as we can see the value of  THISMONTH is 6. What if we want to get and display like 06
So we need to modify the date function output to get two digits output .
THISMONTH= PUT(MONTH(TODAY()),Z2.);
HDR = TRIM(LEFT(THISYEAR))|| PUT(LASTMONTH,Z2.);
Explanation:
Option NOCENTER; is SAS statement which aligns the output. Lets not be bothered about that.
We can see the data step starts with DATA RYAN;
Since no input file is used here, so we do not have infile and input statements here.
Today() is a function in SAS. It can be used with various combinations to give us the dates we want.
PROC PRINT DATA=RYAN is the proc step. We are using the keyword VAR to include or select the variables from  the data step to print.

Lets go to the Next step will be to add an input file and print its contents.
//SAS01     EXEC SAS
//POLIN    DD DSN=I15122.TEST.FILE,DISP=SHR <== input dataset
//WORK      DD SPACE=(CYL,(50,10),RLSE)
//SYSIN     DD *                                            
OPTION NOCENTER;                                                  
OPTION SORTLIB='';

DATA RYAN;       
  INFILE POLIN;
  INPUT @01 CITY   $CHAR02.
        @06 DATE   $CHAR08.
        @14 STATE  $CHAR02.
        @16 AMT    COMMA9.2;
  PROC PRINT DATA= RYAN;

This is how the input looks like.
Input File:
----+----1----+----2----+-
**************************
CA00020130320WB100000.55 
CA00020130320WB200000.55 
MI00020130120KA300000.55 
MI00020130320AP400000.55 
CA00020130120TN500000.55 
CA00020130320KA600000.55 
RR00020130120AP700000.55 
ST00020130320TN800000.55 
**************************

Output:
The SAS System
Obs    CITY      DATE      STATE       AMT  
 1      CA     20130320     WB      100000.55
 2      CA     20130320     WB      200000.55
 3      MI     20130120     KA      300000.55
 4      MI     20130320     AP      400000.55
 5      CA     20130120     TN      500000.55
 6      CA     20130320     KA      600000.55
 7      RR     20130120     AP      700000.55
 8      ST     20130320     TN      800000.55


The fist column is inserted by SAS in the output called observation column,ie number of rows processed.Use of NOOBS in the proc statement will suppress the First column.
If we would run it with NODUPKEYS with Key on the CITY, then it would remove all the duplicate key values.The below code shows how PROC SORT can be used to sort out the data and filter out duplicate.. We need to give one key value if we use NODUPKEYS. If we do not give we will get the below error.
ERROR: No BY statement used or no BY variables specified. A BY statement must be used with variable names to sort on.
DATA RYAN;       
  INFILE POLIN;
  INPUT @01 CITY   $CHAR02.
        @06 DATE   $CHAR08.
        @14 STATE  $CHAR02.
        @16 AMT    COMMA9.2;
  PROC SORT DATA= RYAN NODUPKEYS;

       BY CITY;
     PROC PRINT DATA= RYAN
Output:
Obs    CITY      DATE      STATE       AMT
 1      CA     20130320     WB      100000.55
 2      MI     20130120     KA      300000.55
 3      RR     20130120     AP      700000.55
 4      ST     20130320     TN      800000.55

How ever we can also use NODUPS; This will check the entire observation and filter out only if entire observation is duplicate.

Limiting the number of observations in SAS
Suppose You have a input of million records , but you want to proceed or test your code with 10 records. In this scenario, it is advisable to use OBS=number-of-records  along with the infile statement. This will restrict the number of records to 10
DATA RYAN;       
  INFILE POLIN OBS=10 ;
....

Using a simple IF loop and THEN OUTPUT in SAS
lets add the below line before we print the SAS data using PROC PRINT.
IF(( CITY NE 'RR') OR (CITY='SS')) THEN OUTPUT;
PROC PRINT DATA=RYAN;

It wil print all the records except the observation with city value of RR.

Next,Lets do some more modifications to make the output dataset more meaningful:
Say, we want to add one more column which will tell us the city name in full, ie for the observation where the CITY is MI, it should say MICHIGAN.
 So we need to add one more variable which will be added in the output. 
INFILE POLIN;
  INPUT @01 CITY   $CHAR02.
        @06 DATE   $CHAR08.
        @14 STATE  $CHAR02.
        @16 AMT    COMMA9.2;

LENGTH CITY_NAME $20;
IF CITY = 'RR' THEN CITY_NAME ='ROCK VINE' ;
ELSE                                        
IF CITY = 'MI' THEN CITY_NAME ='MICHIGAN' ; 
ELSE                                        
IF CITY = 'CA' THEN CITY_NAME ='CALIFORNIA'; 

     PROC PRINT DATA= RYAN;
       VAR CITY DATE STATE AMT CITY_NAME; 
Here goes the output:

CITY      DATE      STATE       AMT       CITY_NAME
 CA     20130320     WB      100000.55    CALIFORNI
 CA     20130320     WB      200000.55    CALIFORNI
 MI     20130120     KA      300000.55    MICHIGAN 
 MI     20130320     AP      400000.55    MICHIGAN 
 CA     20130120     TN      500000.55    CALIFORNI
 CA     20130320     KA      600000.55    CALIFORNI
 RR     20130120     AP      700000.55    ROCK VINE
 ST     20130320     TN      800000.55             


Why did we add the line LENGTH CITY_NAME $20; ???. Ideally our job would have run without that part also, but the City name would have been truncated. Using that line we are making sure it takes 20 bytes and no truncation happens.!
To be continued.....  SAS in Mainframes(z/Os)  - Part 2  !! drop a note if u liked it.

40 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
    Replies
    1. Hei.. did nt find any such book for mainframes..all are windows based.

      Delete
  2. Main Frames Online Training, ONLINE TRAINING – IT SUPPORT – CORPORATE TRAINING http://www.21cssindia.com/courses/main-frames-online-training-107.html The 21st Century Software Solutions of India offers one of the Largest conglomerations of Software Training, IT Support, Corporate Training institute in India - +919000444287 - +917386622889 - Visakhapatnam,Hyderabad Main Frames Online Training, Main Frames Training, Main Frames, Main Frames Online Training| Main Frames Training| Main Frames| "Courses at 21st Century Software Solutions
    Talend Online Training -Hyperion Online Training - IBM Unica Online Training - Siteminder Online Training - SharePoint Online Training - Informatica Online Training - SalesForce Online Training - Many more… | Call Us +917386622889 - +919000444287 - contact@21cssindia.com
    Visit: http://www.21cssindia.com/courses.html"

    ReplyDelete
  3. It was really a nice article and i was really impressed by reading thisarticle We are also giving all software Course Online Training.The Sas Online Training is one of the leading Online Training institute in the world.

    ReplyDelete
  4. any one can provide me the link for downloading SAS Setup for mainframes OS

    ReplyDelete
  5. Nice and Good Colletcive Information on SAS Online Training it is most use full for Fresher Student who wants to Learn SAS By Online Courses

    ReplyDelete
  6. This is awesome. I am new for SAS. I am searching document exactly like this. Crispy and clear. Thank you very much.

    ReplyDelete
  7. really good fr ppl new to dis ..canu refer any tutorial website fr SAS-mainframes

    ReplyDelete
    Replies
    1. Thanks!. I Will check and let you know the same

      Delete
  8. This is a very good example and useful for as and to get more knowledge for this explaination and use for developed your business sasonlinetraining

    ReplyDelete
  9. can anyone help with XML parsing in Mainframe SAS....want to read an XML file and print it into a SAS dataset....

    ReplyDelete
    Replies
    1. Hi, Why dont u parse the xml statements with DFSORT and put in a dataset and then sas to do the rest with SAS. Ideally hierarchical data(like xml) cant be read into sas. Windows version of SAS has xml utilities which helps, but with Z/OS it becomes difficult to interpret. I had one link which might be of help to you. Have a look and let me know.
      http://www.lexjansen.com/wuss/2003/HOW/i-reading_and_writing_xml.pdf
      Meantime if i can gethold of some quick codes will post here.

      Delete
    2. I tried using the codes given in the above link but XML engine in mainframe is not able to transcode it and throws error.

      Delete
  10. Error: some code points did not Transcode.

    ReplyDelete
  11. Very nice article...looking forward to read more about SAS and from you. Thank you!!

    ReplyDelete
  12. Thank you for such a simple though in detail article on SAS for a beginner...

    ReplyDelete
  13. Thanks a million for this useful and well explained notes. I'm new to SAS and it has helped me a lot.

    ReplyDelete
  14. This is my first visit to your blog, your post made productive reading, thank you. dot net training in chennai

    ReplyDelete
  15. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in TECHNOLOGY , kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on TECHNOLOGY. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us.
    Saurabh Srivastava
    MaxMunus
    E-mail: saurabh@maxmunus.com
    Skype id: saurabhmaxmunus
    Ph:+91 8553576305 / 080 - 41103383
    http://www.maxmunus.com/


    ReplyDelete
  16. Great article. Really helpful for beginners in SAS

    ReplyDelete
  17. There are lots of information about latest technology and how to get trained in them, like this have spread around the web, but this is a unique one according to me. The strategy you have updated here will make me to get trained in future technologies. By the way you are running a great blog. Thanks for sharing this.

    Base SAS Training in Chennai

    MSBI Training in Chennai

    ReplyDelete
  18. I read your blog completely i got lot of information in this blog. The strategy you update in this blog is very nice .Thanks for sharing this blog. keep sharing more blogs.

    SAS Training in Bangalore

    ReplyDelete
  19. I really appreciate the information shared above. It’s of great help. If someone wants to learn Online (Virtual) instructor lead live training in IBM Mainframe TECHNOLOGY, kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor-led training on TECHNOLOGY. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ pieces of training in India, USA, UK, Australia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us.
    Pratik Shekhar
    MaxMunus
    E-mail: pratik@maxmunus.com
    Ph:(0) +91 9066268701
    http://www.maxmunus.com/

    ReplyDelete
  20. Informative article, just what I was looking for.seo services chennai

    ReplyDelete
  21. Wow amazing i think these are the mistakes i have repeatedly doing when writing with the content. Especially by checking with the spelling i am spending much time. And some distractions likewise you are telling by checking with the email and so on. I hope it will be useful for me in future. Please keep update like this

    Hadoop Training in Chennai

    Base SAS Training in Chennai

    ReplyDelete
  22. This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
    SAS Training in Chennai

    ReplyDelete
  23. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    Android Training in Chennai
    Ios Training in Chennai

    ReplyDelete
    Replies
    1. Hi Sunitha,
      can you let me know how Android and Ios (advertised by you above) is related to SAS?

      Delete
  24. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.

    SAS Online Training |
    Tableau Online Training|
    R Programming Online Training|

    ReplyDelete
  25. Interesting blog post.This blog shows that you have a great future as a content writer.waiting for more updates...
    Digital Marketing Company in India

    ReplyDelete
  26. Hi, thank you for sharing this pretty useful blog, from the IT industry survey, Big Data/Analytics is the hot trend with the strong demand of talent in the upcoming years; I think you are making a great choice to pursue a Master degree in this area.
    Regards,SAS Training in Chennai

    ReplyDelete
  27. Sas Training Institute in Noida- Webtrackker is the best SAS training institute in noida. SAS has an integrated system that allows us to fire our SAS programs with a detailed review of each intermediate data set. What a concept! Without this great feature, you should find the wheel again for each condition, select variables, and sort the query data sets for the debugging goal of each step. Who can remember all these details or have time to re-type the SAS code.
    Sap Training Institute in Noida
    PHP Training Institute in Noida
    Hadoop Training Institute in Noida
    Oracle Training Institute in Noida
    Linux Training Institute in Noida
    Dot net Training Institute in Noida
    Salesforce training institute in noida
    Java training institute in noida

    ReplyDelete
  28. Hi, this is really nice.

    I have a question on SASLIST.

    I have a SAS datasetp that reads a flat text file. The file is huge.

    I do not have a PROC PRINT step, but still SASLIST displays the contents of the file. I don't need SASLIST. How can I prevent SAS from doing this?

    I even removed the SASLIST DD step, but still SASLIST comes.

    ReplyDelete
  29. Nice blog..! I really loved reading through this article... Thanks for sharing such an amazing post with us and keep blogging...
    ios app development course
    ios app training in bangalore

    ReplyDelete