Hi,
I am setting two data sets.Please find my code below :
data login_oct_dec;
set login_old Oct_Dec_Data;
if (date > '2020-10-01' and date < '2020-12-31');
run;
In this error is : variable date is character and numeric both.
Using proc contents , i found that in login_old , date is char while in Oct_Dec_Data, date is num.
Please help.
Probably best is to convert the character version to a date value. That, from your code would look like:
data login_oct_dec; set login_old (rename=(date=chardate)) Oct_Dec_Data; if not missing(chardate) then date=input(chardate,yymmdd10.); run;
Character comparisons involving > or < are mostly a bad idea.
To specify literal values for dates in comparisons the appearance is a quoted DATE9. followed by d. Date9 means ddMONyyyy or 01JAN2021, and the literal value to use in code would be '01Jan2021'd.
Question though, did you intend to exclude 01OCT2020 and 31DEC2020? That is what the < and > would do.
If you want all the records with dates including 01OCT2020 and 31DEC2020 you would use:
if '01OCT2020'd le date le '31DEC2020'd;
if you actually do not want the end points:
if '01OCT2020'd lt date lt '31DEC2020'd; (or use < instead of lt )
Probably best is to convert the character version to a date value. That, from your code would look like:
data login_oct_dec; set login_old (rename=(date=chardate)) Oct_Dec_Data; if not missing(chardate) then date=input(chardate,yymmdd10.); run;
Character comparisons involving > or < are mostly a bad idea.
To specify literal values for dates in comparisons the appearance is a quoted DATE9. followed by d. Date9 means ddMONyyyy or 01JAN2021, and the literal value to use in code would be '01Jan2021'd.
Question though, did you intend to exclude 01OCT2020 and 31DEC2020? That is what the < and > would do.
If you want all the records with dates including 01OCT2020 and 31DEC2020 you would use:
if '01OCT2020'd le date le '31DEC2020'd;
if you actually do not want the end points:
if '01OCT2020'd lt date lt '31DEC2020'd; (or use < instead of lt )
Hello @anandmgjsa I have modified your code as shown below.
This should solve your problem
data login_old;
format date yymmdd10.;
input date yymmdd10.;
datalines;
2020-10-11
2020-10-19
2020-10-01
2020-11-21
2020-12-30
2020-11-13
2020-06-22
;
run;
data login_oct_dec;
format date yymmdd10.;
set login_old ;
if (date > input("2020-10-01",yymmdd10.) and date < input("2020-12-31",yymmdd10.));
run;
The output will be like this
Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.
Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.
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.