SlideShare une entreprise Scribd logo
1  sur  25
Télécharger pour lire hors ligne
Introduction to Where
     Expressions
         Mark Tabladillo, Ph.D.
Software Developer, MarkTab Consulting
Associate Faculty University of Phoenix
          Faculty,
           January 30, 2007
Introduction
• WHERE expressions allow for processing
  subsets of observations
• WHERE expressions can be used in the
  DATA step or with PROC (procedure)
  statements
• This presentation will contain a series of
  features and examples of the WHERE
                    p
  expression
• We end with some intensive macros
WHERE-expression Processing
 WHERE expression
• Enables us to conditionally select a subset
  of observations, so that SAS processes
  only the observations that meet a set of
  specified conditions.




http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a000999253.htm
Work Sales Dataset
     Work.Sales
data work.sales (drop=i randomState);
    length state $2 sales 8 randomState 3;
    do i = 1 to 2500;
         randomState = round(rand('gaussian',3,1)+0.5);
         if randomState in (1,2,3,4,5) then do;
                             (        )
               select(randomState);
                    when(1) state='TN';
                    when(2) state='AL';
                    when(3) state= GA ;
                             state='GA';
                    when(4) state='FL';
                    when(5) state='MS';
               end;
               sales = int(rand('gaussian',1000000,500000));
               output work.sales;
         end;
    end;
    run;
Data Set Option or Statement
   data work.highSales;
           set work.sales (where=(sales>1500000));
           run;

   data work highSales;
        work.highSales;
           set work.sales;
           where sales>1500000;
           run;

   proc means data=work.sales;
          where sales>1500000;
          run;
             ;
Data Set Option or Statement
   data work.lowSales;
           set work.sales (where=(sales<0));
           run;

   data work lowSales;
        work.lowSales;
           set work.sales;
           where sales<0;
           run;

   proc means data=work.sales (where=(sales<0));
          run;
Multiple Comparisons
data work.highFloridaSales;
        set work.sales (where=(sales>1500000 and state = 'FL'));
        run;

data work highFloridaSales;
     work.highFloridaSales;
        set work.sales;
        where sales>1500000 and state = 'FL';
        run;

proc freq data=work.sales;
        tables state;
        where sales>1500000 and state = 'FL';
                                            ;
        run;
SAS Functions
data work.highFloridaSales;
        set work.sales (where=(sales>1500000 and substr(state,1,1) = 'F'));
        run;

data work highFloridaSales;
     work.highFloridaSales;
        set work.sales;
        where sales>1500000 and substr(state,1,1) = 'F';
        run;

proc means data=work.sales;
       where sales>1500000 and substr(state,1,1) = 'F';
       run;
          ;
Comparison Operators
Priority     Order of               Symbols Mnemonic
                Evaluation                    Equivalent
Group I      right to left             **
                                       +
                                        -
                                      ˆ¬~   NOT
                                       ><   MIN
                                       <>   MAX


http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a000780367.htm
Comparison Operators
Priority Order of                  Symbols Mnemonic
            Evaluation                      Equivalent
Group II left to right                *
                                      /
Group        left to right            +
  III
                                          -
Group        left to right             || ¦¦ !!
  IV

http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a000780367.htm
Comparison Operators
Priority     Order of               Symbols Mnemonic
                Evaluation                    Equivalent
Group        left to right             <    LT
  V
                                         <=       LE
                                         =        EQ
                                         ¬=       NE
                                         >=       GE
                                         >        GT
                                                  IN

http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a000780367.htm
Comparison Operators
Priority     Order of               Symbols Mnemonic
               Evaluation                    Equivalent
Group        left to right               &        AND
  VI
Group        left to right              |¦!       OR
  VII




http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a000780367.htm
Comparison Operators
data work.extremeNonGeorgia;
        set work.sales
          (where=((sales<0 | sales>1500000) and state in ('TN','AL','FL','MS')));
        run;

data work.extremeNonGeorgia;
        set work.sales;
        where (sales<0 | sales>1500000) and state in ('TN','AL','FL','MS');
        run;

data work.extremeNonGeorgia;
        set work.sales;
                      ;
        where ^ (0 <= sales <= 1500000) & state ne 'GA';
        run;



 http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a000999255.htm
“Between And”
                    Between And
data work.boundedNonGeorgia;
        set work.sales (where=((sales between 1000000 and 1500000) &
                 state in ('TN','AL','FL','MS')));
        run;

data work.boundedNonGeorgia;
        set work.sales;
        where (sales between 1000000 and 1500000) &
                 state in ('TN','AL','FL','MS');
                  t t i ('TN' 'AL' 'FL' 'MS')
        run;




http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a000999255.htm
Contains ?
           data work.LStates;
                   set work.sales (where=(state contains 'L'));
                   run;

           data work LStates;
                work.LStates;
                   set work.sales;
                   where state contains 'L';
                   run;

           data work.LStates;
                   set work.sales;
                   where state ? 'L';
                                    ;
                   run;




http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a000999255.htm
Is Null/Is Missing
            data work.nullStates;
                    set work.sales (where=(state is null));
                    run;

            data work.missingStates;
                    se o sa es (where=(state s ss g));
                    set work.sales ( e e (s a e is missing));
                    run;

            data work.nullSales;
                    set work.sales;
                        work sales;
                    where sales is missing;
                    run;

            data work.nonNullSales;
                    set work.sales;
                    where sales is not missing;
                    run;
http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a000999255.htm
Like
         data work.likeL;
                 set work.sales (where=(state like '%L'));
                      work sales
                 run;

         data work.likeL;
                 set work.sales (where=(state like quot;%Lquot;));
                 run;

         data work likeL;
              work.likeL;
                 set work.sales (where=(state like quot;%%Lquot;));
                 run;

         data work.notLikeG;
                 set work.sales;
                 where state not like 'G_';
                 run;

http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a000999255.htm
Sounds Like (Soundex)
        data work.soundsLikeFill;
                set work.sales (where=(state =* 'fill'));
                run;

        data work notSoundsLikeTin;
             work.notSoundsLikeTin;
                set work.sales;
                where state not =* 'tin';
                run;




http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a000999255.htm
“Same And”
                        Same And
data work.boundedNonGeorgia;
        set work.sales (where=((sales between 1000000 and 1500000) &
                 state in ('TN','AL','FL','MS')));
        run;

data work.boundedNonGeorgia;
        set work.sales;
        where (sales between 1000000 and 1500000);
        where same and state i ('TN' 'AL' 'FL' 'MS')
          h             d t t in ('TN','AL','FL','MS');
        run;

data work.boundedNonGeorgia;
                         g ;
        set work.sales;
        where same and (sales between 1000000 and 1500000);
        where same and state in ('TN','AL','FL','MS');
        run;
http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a000999255.htm
WHERE vs. Subsetting IF
               vs
Task                                                         Method


Make the selection in a procedure without using a            WHERE expression
   preceding DATA step
Take advantage of the efficiency available with an indexed   WHERE expression
   data set
Use one of a group of special operators, such as             WHERE expression
   BETWEEN-AND, CONTAINS, IS MISSING or IS
   NULL, LIKE, SAME-AND, and Sounds-Like
B    th     l ti           thi   th th          i bl   l     b tti
Base the selection on anything other than a variable value subsetting IF
   that already exists in a SAS data set. For example, you
   can select a value that is read from raw data, or a
   value that is calculated or assigned during the course
   of the DATA step
    f th          t
Make the selection at some point during a DATA step          subsetting IF
   rather than at the beginning
Execute the selection conditionally                          subsetting IF

http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a001000521.htm
Intensive Dataset Generation
%macro OurCentury();
%local year interest;
       y             ;
%do year = 2001 %to 2100;
    %let interest = %sysfunc(compound(1,.,0.05,%eval(&year.-2001)));
    data work.sales&year. (drop=i randomState index=(state sales));
         length state $2 stateName $20 sales 8 randomState 3;
             g                                                    ;
         do i = 1 to 2500;
               randomState = round(56*rand('uniform')+0.5);
               if randomState <= 56 and randomState not in (3,7,14,43,52) then do;
                       state = fipstate(randomState);
                                 p     (            )
                       stateName = fipnameL(randomState);
                       sales = int(rand('gaussian',1000000*&interest.,500000*&interest.));
                       output work.sales&year.;
               end;
         end;
         run;
%end;
%mend OurCentury;   y
%OurCentury;
Year/State Datasets
%macro SalesByYearState();
%local year stateCode state;
%do year = 2001 %to 2100;
    %do stateCode = 1 %to 56;
         %if &stateCode ne 3 & &stateCode ne 7 & &stateCode. ne 14 &
             &stateCode.         &stateCode.         &stateCode
              &stateCode. ne 43 & &stateCode. ne 52 %then %do;
              %let state = %sysfunc(fipstate(&stateCode.));
              data work.sales&year.&state.;
                  set work.sales&year.;
                     t    k l &
                  where state = quot;&state.quot;;
                  run;
         %end; ;
    %end;
%end;
%mend SalesByYearState;
%SalesByYearState;
Year/State High Sales Datasets
%macro HighSalesByYearState();
%local year stateCode state interest keepDataset;
%do year = 2001 %to 2100;
     %let interest = %sysfunc(compound(1,.,0.05,%eval(&year.-2001)));
     %do stateCode = 1 %to 56;
           %if &stateCode. ne 3 & &stateCode. ne 7 & &stateCode. ne 14 & &stateCode. ne 43 &
                 &stateCode. ne 52 %then %do;
                 %let state = %sysfunc(fipstate(&stateCode.));
                 %let keepDataset = 0;
                 data work.sales&year.&state.high;
                       set work.sales&year.;
                       where state = quot;&state.quot; and sales > 2000000*&i t
                         h      t t    quot;& t t quot; d l          2000000*&interest.;
                                                                             t
                       call symput('keepDataset',left('1'));
                       run;
                 %if not(&keepDataset.) %then %do;
                       p
                       proc datasets lib=work nolist;
                              delete sales&year.&state.high;
                              run; quit;
                 %end;
           %end;
     %end;
%end;
%mend HighSalesByYearState;
%HighSalesByYearState;
Conclusion
• The WHERE expression allows for
  efficient observation processing in the
  DATA step and the PROC statements
• The SAS System Documentation provides
  specific details on the syntax
• Using macros increases the processing
  power of WHERE expressions
           f                  i
Contact Information
• Mark Tabladillo
  MarkTab Consulting
  http://www.marktab.com/
  http://www marktab com/

Contenu connexe

Similaire à Introduction to SAS System Where Expressions

解决Ora 14098分区交换索引不匹配错误
解决Ora 14098分区交换索引不匹配错误解决Ora 14098分区交换索引不匹配错误
解决Ora 14098分区交换索引不匹配错误
maclean liu
 
foreach (DataRow dataRow in dataTable.Rows) .docx
            foreach (DataRow dataRow in dataTable.Rows)        .docx            foreach (DataRow dataRow in dataTable.Rows)        .docx
foreach (DataRow dataRow in dataTable.Rows) .docx
joyjonna282
 
Developing A Real World Logistic Application With Oracle Application - UKOUG ...
Developing A Real World Logistic Application With Oracle Application - UKOUG ...Developing A Real World Logistic Application With Oracle Application - UKOUG ...
Developing A Real World Logistic Application With Oracle Application - UKOUG ...
Roel Hartman
 
Netezza technicaloverviewportugues
Netezza technicaloverviewportugues Netezza technicaloverviewportugues
Netezza technicaloverviewportugues
hdkid82
 
Html and i_phone_mobile-2
Html and i_phone_mobile-2Html and i_phone_mobile-2
Html and i_phone_mobile-2
tonvanbart
 
Java.script
Java.scriptJava.script
Java.script
g Nama
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applications
elliando dias
 

Similaire à Introduction to SAS System Where Expressions (20)

iOS 개발자의 Flutter 체험기
iOS 개발자의 Flutter 체험기iOS 개발자의 Flutter 체험기
iOS 개발자의 Flutter 체험기
 
解决Ora 14098分区交换索引不匹配错误
解决Ora 14098分区交换索引不匹配错误解决Ora 14098分区交换索引不匹配错误
解决Ora 14098分区交换索引不匹配错误
 
Adding Statistical Functionality to the DATA Step with PROC FCMP
Adding Statistical Functionality to the DATA Step with PROC FCMPAdding Statistical Functionality to the DATA Step with PROC FCMP
Adding Statistical Functionality to the DATA Step with PROC FCMP
 
foreach (DataRow dataRow in dataTable.Rows) .docx
            foreach (DataRow dataRow in dataTable.Rows)        .docx            foreach (DataRow dataRow in dataTable.Rows)        .docx
foreach (DataRow dataRow in dataTable.Rows) .docx
 
Web lab programs
Web lab programsWeb lab programs
Web lab programs
 
Developing A Real World Logistic Application With Oracle Application - UKOUG ...
Developing A Real World Logistic Application With Oracle Application - UKOUG ...Developing A Real World Logistic Application With Oracle Application - UKOUG ...
Developing A Real World Logistic Application With Oracle Application - UKOUG ...
 
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
jQuery & 10,000 Global Functions: Working with Legacy JavaScriptjQuery & 10,000 Global Functions: Working with Legacy JavaScript
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
 
Scti 2011 minicurso jquery
Scti 2011 minicurso jqueryScti 2011 minicurso jquery
Scti 2011 minicurso jquery
 
SQL -PHP Tutorial
SQL -PHP TutorialSQL -PHP Tutorial
SQL -PHP Tutorial
 
The journey of an (un)orthodox optimization
The journey of an (un)orthodox optimizationThe journey of an (un)orthodox optimization
The journey of an (un)orthodox optimization
 
Netezza technicaloverviewportugues
Netezza technicaloverviewportugues Netezza technicaloverviewportugues
Netezza technicaloverviewportugues
 
Speed of Lightning
Speed of LightningSpeed of Lightning
Speed of Lightning
 
Html and i_phone_mobile-2
Html and i_phone_mobile-2Html and i_phone_mobile-2
Html and i_phone_mobile-2
 
Java.script
Java.scriptJava.script
Java.script
 
Html
HtmlHtml
Html
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applications
 
How to generate a 100+ page website using parameterisation in R
How to generate a 100+ page website using parameterisation in RHow to generate a 100+ page website using parameterisation in R
How to generate a 100+ page website using parameterisation in R
 
Asynchronous Interfaces
Asynchronous InterfacesAsynchronous Interfaces
Asynchronous Interfaces
 
Chris Mc Glothen Sql Portfolio
Chris Mc Glothen Sql PortfolioChris Mc Glothen Sql Portfolio
Chris Mc Glothen Sql Portfolio
 
Oracle SQL Basics
Oracle SQL BasicsOracle SQL Basics
Oracle SQL Basics
 

Plus de Mark Tabladillo

Plus de Mark Tabladillo (20)

How to find low-cost or free data science resources 202006
How to find low-cost or free data science resources 202006How to find low-cost or free data science resources 202006
How to find low-cost or free data science resources 202006
 
Microsoft Build 2020: Data Science Recap
Microsoft Build 2020: Data Science RecapMicrosoft Build 2020: Data Science Recap
Microsoft Build 2020: Data Science Recap
 
201909 Automated ML for Developers
201909 Automated ML for Developers201909 Automated ML for Developers
201909 Automated ML for Developers
 
201908 Overview of Automated ML
201908 Overview of Automated ML201908 Overview of Automated ML
201908 Overview of Automated ML
 
201906 01 Introduction to ML.NET 1.0
201906 01 Introduction to ML.NET 1.0201906 01 Introduction to ML.NET 1.0
201906 01 Introduction to ML.NET 1.0
 
201906 04 Overview of Automated ML June 2019
201906 04 Overview of Automated ML June 2019201906 04 Overview of Automated ML June 2019
201906 04 Overview of Automated ML June 2019
 
201906 03 Introduction to NimbusML
201906 03 Introduction to NimbusML201906 03 Introduction to NimbusML
201906 03 Introduction to NimbusML
 
201906 02 Introduction to AutoML with ML.NET 1.0
201906 02 Introduction to AutoML with ML.NET 1.0201906 02 Introduction to AutoML with ML.NET 1.0
201906 02 Introduction to AutoML with ML.NET 1.0
 
201905 Azure Databricks for Machine Learning
201905 Azure Databricks for Machine Learning201905 Azure Databricks for Machine Learning
201905 Azure Databricks for Machine Learning
 
201905 Azure Certification DP-100: Designing and Implementing a Data Science ...
201905 Azure Certification DP-100: Designing and Implementing a Data Science ...201905 Azure Certification DP-100: Designing and Implementing a Data Science ...
201905 Azure Certification DP-100: Designing and Implementing a Data Science ...
 
Big Data Advanced Analytics on Microsoft Azure 201904
Big Data Advanced Analytics on Microsoft Azure 201904Big Data Advanced Analytics on Microsoft Azure 201904
Big Data Advanced Analytics on Microsoft Azure 201904
 
Managing Enterprise Data Science 201904
Managing Enterprise Data Science 201904Managing Enterprise Data Science 201904
Managing Enterprise Data Science 201904
 
Training of Python scikit-learn models on Azure
Training of Python scikit-learn models on AzureTraining of Python scikit-learn models on Azure
Training of Python scikit-learn models on Azure
 
Big Data Adavnced Analytics on Microsoft Azure
Big Data Adavnced Analytics on Microsoft AzureBig Data Adavnced Analytics on Microsoft Azure
Big Data Adavnced Analytics on Microsoft Azure
 
Advanced Analytics with Power BI 201808
Advanced Analytics with Power BI 201808Advanced Analytics with Power BI 201808
Advanced Analytics with Power BI 201808
 
Microsoft Cognitive Toolkit (Atlanta Code Camp 2017)
Microsoft Cognitive Toolkit (Atlanta Code Camp 2017)Microsoft Cognitive Toolkit (Atlanta Code Camp 2017)
Microsoft Cognitive Toolkit (Atlanta Code Camp 2017)
 
Machine learning services with SQL Server 2017
Machine learning services with SQL Server 2017Machine learning services with SQL Server 2017
Machine learning services with SQL Server 2017
 
Microsoft Technologies for Data Science 201612
Microsoft Technologies for Data Science 201612Microsoft Technologies for Data Science 201612
Microsoft Technologies for Data Science 201612
 
How Big Companies plan to use Our Big Data 201610
How Big Companies plan to use Our Big Data 201610How Big Companies plan to use Our Big Data 201610
How Big Companies plan to use Our Big Data 201610
 
Georgia Tech Data Science Hackathon September 2016
Georgia Tech Data Science Hackathon September 2016Georgia Tech Data Science Hackathon September 2016
Georgia Tech Data Science Hackathon September 2016
 

Dernier

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Dernier (20)

Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 

Introduction to SAS System Where Expressions

  • 1. Introduction to Where Expressions Mark Tabladillo, Ph.D. Software Developer, MarkTab Consulting Associate Faculty University of Phoenix Faculty, January 30, 2007
  • 2. Introduction • WHERE expressions allow for processing subsets of observations • WHERE expressions can be used in the DATA step or with PROC (procedure) statements • This presentation will contain a series of features and examples of the WHERE p expression • We end with some intensive macros
  • 3. WHERE-expression Processing WHERE expression • Enables us to conditionally select a subset of observations, so that SAS processes only the observations that meet a set of specified conditions. http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a000999253.htm
  • 4. Work Sales Dataset Work.Sales data work.sales (drop=i randomState); length state $2 sales 8 randomState 3; do i = 1 to 2500; randomState = round(rand('gaussian',3,1)+0.5); if randomState in (1,2,3,4,5) then do; ( ) select(randomState); when(1) state='TN'; when(2) state='AL'; when(3) state= GA ; state='GA'; when(4) state='FL'; when(5) state='MS'; end; sales = int(rand('gaussian',1000000,500000)); output work.sales; end; end; run;
  • 5. Data Set Option or Statement data work.highSales; set work.sales (where=(sales>1500000)); run; data work highSales; work.highSales; set work.sales; where sales>1500000; run; proc means data=work.sales; where sales>1500000; run; ;
  • 6. Data Set Option or Statement data work.lowSales; set work.sales (where=(sales<0)); run; data work lowSales; work.lowSales; set work.sales; where sales<0; run; proc means data=work.sales (where=(sales<0)); run;
  • 7. Multiple Comparisons data work.highFloridaSales; set work.sales (where=(sales>1500000 and state = 'FL')); run; data work highFloridaSales; work.highFloridaSales; set work.sales; where sales>1500000 and state = 'FL'; run; proc freq data=work.sales; tables state; where sales>1500000 and state = 'FL'; ; run;
  • 8. SAS Functions data work.highFloridaSales; set work.sales (where=(sales>1500000 and substr(state,1,1) = 'F')); run; data work highFloridaSales; work.highFloridaSales; set work.sales; where sales>1500000 and substr(state,1,1) = 'F'; run; proc means data=work.sales; where sales>1500000 and substr(state,1,1) = 'F'; run; ;
  • 9. Comparison Operators Priority Order of Symbols Mnemonic Evaluation Equivalent Group I right to left ** + - ˆ¬~ NOT >< MIN <> MAX http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a000780367.htm
  • 10. Comparison Operators Priority Order of Symbols Mnemonic Evaluation Equivalent Group II left to right * / Group left to right + III - Group left to right || ¦¦ !! IV http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a000780367.htm
  • 11. Comparison Operators Priority Order of Symbols Mnemonic Evaluation Equivalent Group left to right < LT V <= LE = EQ ¬= NE >= GE > GT IN http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a000780367.htm
  • 12. Comparison Operators Priority Order of Symbols Mnemonic Evaluation Equivalent Group left to right & AND VI Group left to right |¦! OR VII http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a000780367.htm
  • 13. Comparison Operators data work.extremeNonGeorgia; set work.sales (where=((sales<0 | sales>1500000) and state in ('TN','AL','FL','MS'))); run; data work.extremeNonGeorgia; set work.sales; where (sales<0 | sales>1500000) and state in ('TN','AL','FL','MS'); run; data work.extremeNonGeorgia; set work.sales; ; where ^ (0 <= sales <= 1500000) & state ne 'GA'; run; http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a000999255.htm
  • 14. “Between And” Between And data work.boundedNonGeorgia; set work.sales (where=((sales between 1000000 and 1500000) & state in ('TN','AL','FL','MS'))); run; data work.boundedNonGeorgia; set work.sales; where (sales between 1000000 and 1500000) & state in ('TN','AL','FL','MS'); t t i ('TN' 'AL' 'FL' 'MS') run; http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a000999255.htm
  • 15. Contains ? data work.LStates; set work.sales (where=(state contains 'L')); run; data work LStates; work.LStates; set work.sales; where state contains 'L'; run; data work.LStates; set work.sales; where state ? 'L'; ; run; http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a000999255.htm
  • 16. Is Null/Is Missing data work.nullStates; set work.sales (where=(state is null)); run; data work.missingStates; se o sa es (where=(state s ss g)); set work.sales ( e e (s a e is missing)); run; data work.nullSales; set work.sales; work sales; where sales is missing; run; data work.nonNullSales; set work.sales; where sales is not missing; run; http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a000999255.htm
  • 17. Like data work.likeL; set work.sales (where=(state like '%L')); work sales run; data work.likeL; set work.sales (where=(state like quot;%Lquot;)); run; data work likeL; work.likeL; set work.sales (where=(state like quot;%%Lquot;)); run; data work.notLikeG; set work.sales; where state not like 'G_'; run; http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a000999255.htm
  • 18. Sounds Like (Soundex) data work.soundsLikeFill; set work.sales (where=(state =* 'fill')); run; data work notSoundsLikeTin; work.notSoundsLikeTin; set work.sales; where state not =* 'tin'; run; http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a000999255.htm
  • 19. “Same And” Same And data work.boundedNonGeorgia; set work.sales (where=((sales between 1000000 and 1500000) & state in ('TN','AL','FL','MS'))); run; data work.boundedNonGeorgia; set work.sales; where (sales between 1000000 and 1500000); where same and state i ('TN' 'AL' 'FL' 'MS') h d t t in ('TN','AL','FL','MS'); run; data work.boundedNonGeorgia; g ; set work.sales; where same and (sales between 1000000 and 1500000); where same and state in ('TN','AL','FL','MS'); run; http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a000999255.htm
  • 20. WHERE vs. Subsetting IF vs Task Method Make the selection in a procedure without using a WHERE expression preceding DATA step Take advantage of the efficiency available with an indexed WHERE expression data set Use one of a group of special operators, such as WHERE expression BETWEEN-AND, CONTAINS, IS MISSING or IS NULL, LIKE, SAME-AND, and Sounds-Like B th l ti thi th th i bl l b tti Base the selection on anything other than a variable value subsetting IF that already exists in a SAS data set. For example, you can select a value that is read from raw data, or a value that is calculated or assigned during the course of the DATA step f th t Make the selection at some point during a DATA step subsetting IF rather than at the beginning Execute the selection conditionally subsetting IF http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a001000521.htm
  • 21. Intensive Dataset Generation %macro OurCentury(); %local year interest; y ; %do year = 2001 %to 2100; %let interest = %sysfunc(compound(1,.,0.05,%eval(&year.-2001))); data work.sales&year. (drop=i randomState index=(state sales)); length state $2 stateName $20 sales 8 randomState 3; g ; do i = 1 to 2500; randomState = round(56*rand('uniform')+0.5); if randomState <= 56 and randomState not in (3,7,14,43,52) then do; state = fipstate(randomState); p ( ) stateName = fipnameL(randomState); sales = int(rand('gaussian',1000000*&interest.,500000*&interest.)); output work.sales&year.; end; end; run; %end; %mend OurCentury; y %OurCentury;
  • 22. Year/State Datasets %macro SalesByYearState(); %local year stateCode state; %do year = 2001 %to 2100; %do stateCode = 1 %to 56; %if &stateCode ne 3 & &stateCode ne 7 & &stateCode. ne 14 & &stateCode. &stateCode. &stateCode &stateCode. ne 43 & &stateCode. ne 52 %then %do; %let state = %sysfunc(fipstate(&stateCode.)); data work.sales&year.&state.; set work.sales&year.; t k l & where state = quot;&state.quot;; run; %end; ; %end; %end; %mend SalesByYearState; %SalesByYearState;
  • 23. Year/State High Sales Datasets %macro HighSalesByYearState(); %local year stateCode state interest keepDataset; %do year = 2001 %to 2100; %let interest = %sysfunc(compound(1,.,0.05,%eval(&year.-2001))); %do stateCode = 1 %to 56; %if &stateCode. ne 3 & &stateCode. ne 7 & &stateCode. ne 14 & &stateCode. ne 43 & &stateCode. ne 52 %then %do; %let state = %sysfunc(fipstate(&stateCode.)); %let keepDataset = 0; data work.sales&year.&state.high; set work.sales&year.; where state = quot;&state.quot; and sales > 2000000*&i t h t t quot;& t t quot; d l 2000000*&interest.; t call symput('keepDataset',left('1')); run; %if not(&keepDataset.) %then %do; p proc datasets lib=work nolist; delete sales&year.&state.high; run; quit; %end; %end; %end; %end; %mend HighSalesByYearState; %HighSalesByYearState;
  • 24. Conclusion • The WHERE expression allows for efficient observation processing in the DATA step and the PROC statements • The SAS System Documentation provides specific details on the syntax • Using macros increases the processing power of WHERE expressions f i
  • 25. Contact Information • Mark Tabladillo MarkTab Consulting http://www.marktab.com/ http://www marktab com/