BookmarkSubscribeRSS Feed
Daily1
Quartz | Level 8

data emp;
input EMPMO $ 1-4 ENAME $ 6-11 JOB $ 13-21 MGR HIREDATE :date11. SAL DEPTNO COMM ;
format hiredate date11.;
infile datalines missover;
datalines;
7369 SMITH CLERK 7902 17-Dec-80 800 20
7499 ALLEN SALESMAN 7698 20-Feb-81 1600 30 300
7521 WARD SALESMAN 7698 22-Feb-81 1250 30 500
7566 JONES MANAGER 7839 02-Apr-81 2975 20
7654 MARTIN SALESMAN 7698 28-Sep-81 1250 30 1400
7698 BLAKE MANAGER 7839 01-May-81 2850 30
7782 CLARK MANAGER 7839 09-Jun-81 2450 10
7788 SCOTT ANALYST 7566 09-Dec-81 3000 20
7839 KING PRESIDENT   17-Nov-81 5000 10
7844 TURNER SALESMAN 7698 08-Sep-81 1500 30
7876 ADAMS CLERK 7788 12-Jan-83 1100 20
7900 JAME CLERK 7698 03-Dec-81 950 30
7902 FORD ANALYST 7566 03-Dec-81 3000 20
7934 MILLER CLERK 7782 23-Jan-82 1300 10
;

 

Q) List the emps who joined before 1981.?

7 REPLIES 7
PaigeMiller
Diamond | Level 26

The first important step is to read the data properly. If you have looked at the log, you would see a number of NOTEs which cannot be ignored; meaning that this code

 

input EMPMO $ 1-4 ENAME $ 6-11 JOB $ 13-21 MGR HIREDATE :date11. SAL DEPTNO COMM ;

does not read the data properly, and produces a lot of missing values in the data set. So, please, fix the code, examine the log, examine the actual data set, and once that is working you should be able to continue.

 

Once that is cleaned up, a command such as

 

where year(hiredate)<1981;

in a PROC or in a DATA step ought to give you the proper results.

--
Paige Miller
Daily1
Quartz | Level 8
data emp;
input EMPNO $ 1-4 ENAME $ 6-11 JOB $ 13-21 MGR HIREDATE :date11. SAL DEPTNO COMM ;
format hiredate date11.;
infile datalines missover;
datalines;
7369 SMITH  CLERK     7902 17-Dec-80 800  20     
7499 ALLEN  SALESMAN  7698 20-Feb-81 1600 30 300 
7521 WARD   SALESMAN  7698 22-Feb-81 1250 30 500 
7566 JONES  MANAGER   7839 02-Apr-81 2975 20     
7654 MARTIN SALESMAN  7698 28-Sep-81 1250 30 1400
7698 BLAKE  MANAGER   7839 01-May-81 2850 30     
7782 CLARK  MANAGER   7839 09-Jun-81 2450 10     
7788 SCOTT  ANALYST   7566 09-Dec-81 3000 20     
7839 KING   PRESIDENT .    17-Nov-81 5000 10     
7844 TURNER SALESMAN  7698 08-Sep-81 1500 30     
7876 ADAMS  CLERK     7788 12-Jan-83 1100 20     
7900 JAME   CLERK     7698 03-Dec-81 950  30     
7902 FORD   ANALYST   7566 03-Dec-81 3000 20     
7934 MILLER CLERK     7782 23-Jan-82 1300 10     
;

proc sql;
select* from emp where empno in (select mgr from emp);

 

 

PaigeMiller
Diamond | Level 26

Ok, that works too, instead of fixing the INPUT statement, you can clean up the data itself.

--
Paige Miller
Daily1
Quartz | Level 8
proc sql;
select* from emp where empno in (select mgr from emp);
This code did not work on my compiler
Output:
proc sql;
74 select* from emp where empno in (select mgr from emp);
ERROR: Expression using IN has components that are of different data types.
NOTE: The IN referred to may have been transformed from an OR to an IN at some point during PROC SQL WHERE clause optimization.
NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.
PaigeMiller
Diamond | Level 26

@Daily1 wrote:
proc sql;
select* from emp where empno in (select mgr from emp);
This code did not work on my compiler
Output:
proc sql;
74 select* from emp where empno in (select mgr from emp);
ERROR: Expression using IN has components that are of different data types.
NOTE: The IN referred to may have been transformed from an OR to an IN at some point during PROC SQL WHERE clause optimization.
NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.

EMPNO is character, MGR is numeric. For this code to work they must both be character or both be numeric.

--
Paige Miller
Daily1
Quartz | Level 8
proc sql ;
select* from emp where hiredate '01-jan-81' and '31-dec-81';
ballardw
Super User

@Daily1 wrote:
proc sql ;
select* from emp where hiredate '01-jan-81' and '31-dec-81';

Does not work.

 

Your Hiredate, read with a date11 informat is a SAS date value. If you are going to compare a SAS date value to a specific value the format is '01JAN81'd or '01JAN2018'd. The quotes plus the trailing D tell SAS you want to use a date literal value.

 

Comparison of date to string '01-Jan-81';

223  data junk;
224    x='01Jan1981'd;
225    format x date11.;
226    if x= '01-Jan-81' then put 'Match';
227  run;

NOTE: Character values have been converted to numeric values at the places given by:
      (Line):(Column).
      226:9
NOTE: Invalid numeric data, '01-Jan-81' , at line 226 column 9.

Second is the incorrect use of 'and' in "hiredate '01-jan-81' and '31-dec-81';"

If you want to test if the hiredate is one of either of the values then:

hiredate in ( '01-jan-81'd '31-dec-81'd);

Though I would suggest ALWAYS using 4 digit years.

hiredate in ( '01jan1981'd '31dec1981'd);

The way you used 'and' originally only compared Hiredate to the first value.

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 918 views
  • 2 likes
  • 3 in conversation