BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
anandmgjsa
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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 )

 

View solution in original post

2 REPLIES 2
ballardw
Super User

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 )

 

Sajid01
Meteorite | Level 14

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

output.PNG

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 461 views
  • 1 like
  • 3 in conversation