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

Hello team,

I have this code and I can't find anything in google search as how to convert date to numeric to use it in year function. Year function only accepts numeric argument:

data mydata;
set readfromhere;
date1= Input(Dateofserv, mmddyy10.);
Where year(date1)=2021;
run;

It says that year function accepts numeric in year function. No idea how to convert DateofServ into numeric.

 

Regards,

Aauslie 

Blue Blue
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

The data step Where really only uses variables from the data set on the SET statement. You should have gotten an error unless the variable DATE1 already existed in your data.

Since you are adding a variable you would use

IF year(date1)=2021;

to subset data on the output data set.

 

A very brief example:

22   data have;
23      x='12/31/2021';
24   run;

NOTE: The data set WORK.HAVE has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.04 seconds
      cpu time            0.01 seconds


25
26   data example1;
27     set have;
28      date1= Input(x, mmddyy10.);
29      Where year(date1)=2021;
ERROR: Variable date1 is not on file WORK.HAVE.
30      format date1 mmddyy10.;
31   run;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.EXAMPLE1 may be incomplete.  When this step was stopped there were 0
         observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


32
33   data example2;
34     set have;
35      date1= Input(x, mmddyy10.);
36      if year(date1)=2021;
37      format date1 mmddyy10.;
38   run;

NOTE: There were 1 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.EXAMPLE2 has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

 

View solution in original post

4 REPLIES 4
ballardw
Super User

The data step Where really only uses variables from the data set on the SET statement. You should have gotten an error unless the variable DATE1 already existed in your data.

Since you are adding a variable you would use

IF year(date1)=2021;

to subset data on the output data set.

 

A very brief example:

22   data have;
23      x='12/31/2021';
24   run;

NOTE: The data set WORK.HAVE has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.04 seconds
      cpu time            0.01 seconds


25
26   data example1;
27     set have;
28      date1= Input(x, mmddyy10.);
29      Where year(date1)=2021;
ERROR: Variable date1 is not on file WORK.HAVE.
30      format date1 mmddyy10.;
31   run;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.EXAMPLE1 may be incomplete.  When this step was stopped there were 0
         observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


32
33   data example2;
34     set have;
35      date1= Input(x, mmddyy10.);
36      if year(date1)=2021;
37      format date1 mmddyy10.;
38   run;

NOTE: There were 1 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.EXAMPLE2 has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

 

andreas_lds
Jade | Level 19

Please post the log.

Kurt_Bremser
Super User

You do not have an issue with the type conversion, you have an issue with the incorrect use of WHERE, which can only filter on variables present in the incoming dataset(s). The log (Maxim 2) should reveal that.

Use a subsetting IF instead.

Astounding
PROC Star

You have a perfectly good character variable.  It contains the information needed to subset the data.  Why is anything being converted to numeric?  If your character variable is truly in mmddyy10 format, just use:

where substr(dateofServ, 7) = '2021';

If there are variations in how the character variable appears and it isn't always in mmddyy10 format, we would have to explore further and perhaps make the statement slightly longer.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1037 views
  • 6 likes
  • 5 in conversation