Horizontal Or Vertical with PROC SQL Or DATA Step

Horizontal Or Vertical with PROC SQL Or DATA Step

Stacking Up ‐ Horizontal or Vertical with PROC SQL or DATA Step Wisconsin Illinois SAS Users Group Milwaukee 28 June 2017 Charu Shankar Technical Training Specialist SAS Institute Canada Inc. Copyright © SAS Institute Inc. All rights reserved. Agenda 1. Vertical Stacking Data & Proc Steps PROC SQL Set Operators Compare and contrast? 2. Horizontal Stacking Data Step Merge PROC SQL Joins Compare and contrast? 3. Questions 4. Useful Links 5. Contact Stacking up Horizontal or Vertical with PROC SQL Or Data Step, Charu Shankar, SAS, 28 Jun 2017 2Charu Shankar, WIILSU , 28 Jun 2017 About your instructor Charu Shankar trains and develops curriculum in SAS data access, analysis, reporting and big data solutions. For over 20 years Charu has delivered computer language training to SAS customers globally and at the United Nations field office in New Delhi. Skilled in customer needs analysis, Charu recommends the right SAS training to customers. Work blog http://blogs.sas.com/content/author/charushankar/ My yoga & cooking www.charuyoga.com Stacking up Horizontal or Vertical with PROC SQL Or Data Step, Charu Shankar, WIILSU, 28 Jun 2017 Charu Shankar, SAS, 28 Jun 2017 3 Wide or long? Broad or narrow? How do you want to go? Visual Stacking PROC SQL Data Step PROC PROC Horizontal Joins Merge Stacking stack columns and align rows. Vertical stacking Set Concatenate Append Datasets rows and align operators columns. Stacking up Horizontal or Vertical with PROC SQL Or Data Step, Charu Shankar, SAS, 28 Jun 2017 4Charu Shankar, WIILSU , 28 Jun 2017 Now some language Data Step PROC SQL Vertical Concatenate Set Operations Stacking Data Step Concatenate Union Proc Append Outer Union Except Intersect Horizontal Merge Joins Stacking Match Merge Inner Join Non Matches Outer Join Left Join Right Join Full Join Stacking up Horizontal or Vertical with PROC SQL Or Data Step, Charu Shankar, SAS, 28 Jun 2017 5Charu Shankar, WIILSU , 28 Jun 2017 1. Vertical Stacking Stacking up Horizontal or Vertical with PROC SQL Or Data Step, Charu Shankar, SAS, 28 Jun 2017 6Charu Shankar, WIILSU , 28 Jun 2017 1. Vertical Stacking ‐ Proc Append Concepts Stacking up Horizontal or Vertical with PROC SQL Or Data Step, Charu Shankar, SAS, 28 Jun 2017 7Charu Shankar, WIILSU , 28 Jun 2017 1. Vertical Stacking ‐ Proc Append Syntax Stacking up Horizontal or Vertical with PROC SQL Or Data Step, Charu Shankar, SAS, 28 Jun 2017 8Charu Shankar, WIILSU , 28 Jun 2017 1. Vertical Stacking ‐ Proc Append Output Stacking up Horizontal or Vertical with PROC SQL Or Data Step, Charu Shankar, SAS, 28 Jun 2017 9Charu Shankar, WIILSU , 28 Jun 2017 1. Vertical Stacking ‐ Proc Datasets Syntax PROC DATASETS LIBRARY= libref; APPEND BASE = SAS‐data‐set DATA =SAS‐data‐set; RUN; Proc datasets library=work; Append base=emps data=emps2008; Quit; Stacking up Horizontal or Vertical with PROC SQL Or Data Step, Charu Shankar, SAS, 28 Jun 2017 10Charu Shankar, WIILSU , 28 Jun 2017 1. Vertical Stacking ‐ Proc Datasets Output Stacking up Horizontal or Vertical with PROC SQL Or Data Step, Charu Shankar, SAS, 28 Jun 2017 11Charu Shankar, WIILSU , 28 Jun 2017 1. Vertical Stacking ‐ Proc Append /Proc Datasets unique cases Stacking up Horizontal or Vertical with PROC SQL Or Data Step, Charu Shankar, SAS, 28 Jun 2017 12Charu Shankar, WIILSU , 28 Jun 2017 1. Vertical Stacking ‐ Proc Append Tips • Ensure that both datasets have the same exact variables and variable attributes to avoid a ‘messy’ log (i.e. avoid using the FORCE option if at all possible). • Explicitly define the dataset name using the ‘DATA=’ option in the PROC APPEND statement. If ‘DATA=xxxx’ is not specified, SAS uses the dataset most recently created. • Use Proc Append with WORK datasets. Avoid using with permanent datasets. • Designate the larger of the two datasets as the BASE dataset to improve processing efficiency Stacking up Horizontal or Vertical with PROC SQL Or Data Step, Charu Shankar, WIILSU, 28 Jun 2017Charu Shankar, SAS, 28 Jun 2017 13 1. Vertical Stacking Proc Append vs. Proc Datasets WHAT Proc Append Proc Datasets RUN group processing No Supports Run‐group processing‐to submit multiple run groups within the same procedure without ending the procedure. Ends with a quit. Default library Work or User None Data Management No Yes Data Building No for both. Use FORCE option if the data= dataset has a different structure from the Base= dataset Processing time Behind the scenes same code is used so little performance gain should be expected. Stacking up Horizontal or Vertical with PROC SQL Or Data Step, Charu Shankar, SAS, 28 Jun 2017 14Charu Shankar, WIILSU , 28 Jun 2017 1. Vertical Stacking ‐ Data Step Concepts Concatenate like‐structured data sets empsdk and empsfr to create a new data set named empsall1. empsdk First Gender Country Lars M Denmark empsall1 Kari F Denmark First Gender Country Jonas M Denmark Lars M Denmark Kari F Denmark empsfr Jonas M Denmark First Gender Country Pierre M France Pierre M France Sophie F France Sophie F France Both data sets contain the same variables. Stacking up Horizontal or Vertical with PROC SQL Or Data Step, Charu Shankar, WIILSU, 28 Jun 2017Charu Shankar, SAS, 28 Jun 2017 15 1. Vertical Stacking ‐ Data Step Syntax Execution empsdk empsfr First Gender Country First Gender Country Lars M Denmark Pierre M France Kari F Denmark EOF Sophie F France Jonas M Denmark data empsall1; set empsdk empsfr; run; empsall1 First Gender Country PDV Lars M Denmark First Gender Country Kari F Denmark Sophie F France Jonas M Denmark Pierre M France Sophie F France Stacking up Horizontal or Vertical with PROC SQL Or Data Step, Charu Shankar, WIILSU, 28 Jun 2017 Charu Shankar, SAS, 28 Jun 2017 16 1. Vertical Stacking ‐ Data Step Output Viewing the Log Partial SAS Log 145 data empsall1; 146 set empsdk empsfr; 147 run; NOTE: There were 3 observations read from the data set WORK.EMPSDK. NOTE: There were 2 observations read from the data set WORK.EMPSFR. NOTE: The data set WORK.EMPSALL1 has 5 observations and 3 variables. Stacking up Horizontal or Vertical with PROC SQL Or Data Step, Charu Shankar, WIILSU, 28 Jun 2017Charu Shankar, SAS, 28 Jun 2017 17 1. Vertical Stacking ‐ Data Step Advantage Scenario 1 Scenario 2. Scenario 3 Standard data step Build new variables Determine which table contributed concatenate to final results Create new table by Build new variables in the The data step can keep all reading every row in new dataset uncommon columns, plus every table listed on the determine which table contributed set statement to final results. data dataconc; data dataconc; data dataconc; set set set wiilsu.prepsales wiilsu.prepsales wiilsu.prepsales(in=ins) wiilsu.nonsales; wiilsu.nonsales; wiilsu.nonsales(in=inn); run; sales=‘YS’; sales=ins; nonsales=‘YN’; nonsales=inn; run; run; Stacking up Horizontal or Vertical with PROC SQL Or Data Step, Charu Shankar, WIILSU, 28 Jun 2017Charu Shankar, SAS, 28 Jun 2017 18 1. Vertical Stacking Proc Append/Proc Datasets vs Data step ACTION PROC APPEND Data Step Concatenate Handing of different cannot include variables found Uses all variables and assigns variables in datasets only in the DATA= dataset missing values as necessary Number of data sets 2 Any number Speed Proc Append much faster since Slower‐ has to read every data set it doesn’t process observations listed on SET statement from BASE= data set Build new variables Cannot build new variables as almost always the best method for or Create new Table descriptor portion information creating a table from several input in base SAS dataset cannot datasets or building new variables change in the same data step Space Proc Append only reads in SET statement reads in all observations from dataset observations from the datasets being appended. Use Proc being concatenated. Append over the SET statement to save work space. Stacking up Horizontal or Vertical with PROC SQL Or Data Step, Charu Shankar, SAS, 28 Jun 2017 19Charu Shankar, WIILSU , 28 Jun 2017 1. Vertical Stacking ‐ PROC SQL Concepts Set Operator PROC SQL provides traditional set operators from relational algebra: OUTER UNION concatenates the query results. Similar to data step concatenate UNION produces all unique rows from both queries. EXCEPT produces rows that are part of the first query only. INTERSECT produces rows that are common to both query results. Stacking up Horizontal or Vertical with PROC SQL Or Data Step, Charu Shankar, WIILSU, 28 Jun 2017Charu Shankar, SAS, 28 Jun 2017 20 1. Vertical Stacking ‐ PROC SQL Concepts Set Operator Data is stored in 2 tables. Which employees Partial train_a have completed ID Name Date training A or B? 11 Bob 15JUN2012 16 Sam 5JUN2012 Training class A is completed in a single session. Date represents the 14 Pete 21JUN2012 date of training. Partial train_b Name ID SDate EDate Training class B is a multi‐ Bob 11 9JUL2012 13JUL2012 session class. SDate is recorded on the first training Pam 15 25JUL2012 27JUL2012 day. EDate is recorded when Kyle 19 12JUL2012 20JUL2012 the course is complete. Chris 21 29JUL2012 . Stacking up Horizontal or Vertical with PROC SQL Or Data Step, Charu Shankar, WIILSU, 28 Jun 2017Charu Shankar, SAS, 28 Jun 2017 21 1. Vertical Stacking ‐ PROC SQL Concepts Set Operator Set operators use the intermediate result sets from two queries to create a final result set. Intermediate Query 1: result set 1 RS1 Final Result Set ? Set Operator • Query 2: Intermediate result set 2 RS2 Stacking up Horizontal or Vertical with PROC SQL Or Data Step, Charu Shankar, WIILSU, 28 Jun 2017Charu Shankar, SAS, 28 Jun 2017 22 1. Vertical Stacking ‐ PROC SQL Syntax Set Operator general form SELECT … UNION | OUTER UNION | EXCEPT | INTERSECT <ALL><CORR> SELECT …; The modifiers ALL and CORR change the default behavior of the set operators.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    77 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us