Hello! I am new to SAS and cant seem to get past this error after WHERE birthdate > 01/01/2001. When I run this I keep getting an error that says "ERROR: Expression using greater than (>) has components that are of different data types". They all have matching data types of:
birthdate | Num | 8 | MMDDYY10. | birthdate | |
gender | Char | 1 | $1. | $1. | gender |
schoolID | Num | 8 | BEST. | schoolID | |
schoolname | Char | 9 | $9. | $9. | schoolname |
studentID | Num | 8 | BEST. | studentID |
Code:
/*Question 1*/
PROC SQL;
CREATE TABLE Data.master_WithSchoolInfo AS
SELECT A.studentID, A.Birthdate, A.Gender, B.SchoolID, B.SchoolName
FROM Data.master A LEFT JOIN Data.school B
ON A.SchoolID = B.SchoolID;
RUN;
PROC CONTENTS DATA = Data.master_WithSchoolInfo;
RUN;
/*Question 2*/
PROC SQL;
CREATE TABLE Data.master_After2001 AS
SELECT *
FROM Data.master_WithSchoolInfo
WHERE Birthdate > '01/01/2001';
RUN;
PROC CONTENTS DATA = Data.master_WithSchoolAfter2001;
RUN;
Can someone help me figure this out?
Thanks!
Birthdate is likely a numerical variable containing a SAS date value (count of days since 01 Jan 1960). Normally a SAS format gets applied to such variables so when you look at it (print it) then the value becomes human readable.
Execute below. This will tell you of what type the variables are and what formats they use (if any).
proc contents data=Data.master; run;
If above assumption is correct (birthdate being of type numeric containing a SAS date value) then you need to formulate the fixed date in your where clause also in a way which instructs SAS that this is a SAS date value. Below should work.
WHERE Birthdate > '01JAN2001'd
Birthdate is a numeric variable but you are comparing it to a character string - '01/01/2001'. Changing your WHERE clause to use a date constant like so should fix your error:
WHERE Birthdate > '01Dec2001'd
Birthdate is likely a numerical variable containing a SAS date value (count of days since 01 Jan 1960). Normally a SAS format gets applied to such variables so when you look at it (print it) then the value becomes human readable.
Execute below. This will tell you of what type the variables are and what formats they use (if any).
proc contents data=Data.master; run;
If above assumption is correct (birthdate being of type numeric containing a SAS date value) then you need to formulate the fixed date in your where clause also in a way which instructs SAS that this is a SAS date value. Below should work.
WHERE Birthdate > '01JAN2001'd
Thanks so much! It worked!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.