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
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
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
Please post the log.
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.
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.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.