BookmarkSubscribeRSS Feed
fredbell
Fluorite | Level 6
Hello

I am attempting to use Datepart function but in the log i end up with the error:

Error: Function Datepart requires a numeric expression as argument 1

Any suggestions?

Fred
16 REPLIES 16
art297
Opal | Level 21
Are you using it on a SAS datetime variable? The function will not work given a character variable unless the variable is wrapped in an input statement converting it first to a SAS datetime variable.

Art
Paige
Quartz | Level 8
It would help if you give us an example.

All I can tell you is that you are trying to use DATEPART on a character variable, and that causes an error.
fredbell
Fluorite | Level 6
Hello

Trouble with Datepart, any suggestions.

I'm told if i wrap variable in an input statement it will convert it to sas datetime, can anyone provide example?

proc sql;
create table Total_CallsHandled_by_Day as
select datepart(ReportTime), sum(agentcallshandled) as Calls_Handled
from ccar_callshandled;
quit;

Log below

251 proc sql;
252 create table Total_CallsHandled_by_Day as
253 select datepart(ReportTime), sum(agentcallshandled) as Calls_Handled
254 from ccar_callshandled;
ERROR: Function DATEPART requires a numeric expression as argument 1.
255 quit;
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE SQL used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds


ReportTime example 2010-09-30 00:00:00.000000


Please help Message was edited by: fredbell
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
I don't understand why it was necessary to abandon this thread:

http://support.sas.com/forums/thread.jspa?threadID=11462&tstart=0

which has specific information - you are referencing a CHARACTER type variable ReportTime which you must first convert to a NUMERIC (SAS DATETIME type) variable using the INPUT(,) function in your SELECT statement.

Scott Barry
SBBWorks, Inc.
fredbell
Fluorite | Level 6
Hi Scott

I did not abandoned, my last post on that thread mentioned i was successful with that issue and gave example of what i did.

Scott i always appreciate help but sometimes for a newbie it is difficult without an example.

Were in the select statement should this be put, also what >, should i use?

Fred
Reeza
Super User
As someone had mentioned in the last thread your reporttime variable is stored as a character rather than a date.

If you create a new variable with the proper format then you will no longer have this issues. Basically, if you don't fix this issue, you'll have a lot of issues working with the variable trying to calculate durations, or comparing to other dates or formatting to other date time formats...I could go on.

Its bad programming, fix it in the initial data creation/import step if possible.

data example;
wrong='2010-09-30 00:00:00.000000'; *this is what you have;
correct=input(wrong, anydtdtm22.); *this converts it to datetime variable;
format correct datetime18.;
run;

Hope that helps to clarify the issues.
fredbell
Fluorite | Level 6
Hi Reeza

This is what i did

data example;
set ccar_callshandled;
correct=input(ReportTime, anydtdtm22.); *this converts it to datetime variable;
format correct datetime9.;
run;

Old ReportTime 2010-10-10 00:00:00.000000
New ReportTime labled correct in dateset 10OCT2010

Thanks

Fred
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Given all the information and feedback, you chose to treat the "symptom" rather than solving the problem (by changing your literal string -- hoping that a left-to-right byte comparison will still work), which in-turn created another challenge in your variable SELECT -- that is my point. Had you addressed the "character string date-time" problem and converted the variable on input to a SAS numeric-type DATETIME variable, then you would not have needed to register this post.

Scott Barry
fredbell
Fluorite | Level 6
Scott i am just doing the best i can, I searched google advance and web forum as you suggested without coming up with an example i could understand.
I asked several times if you could provide an example but you just kept telling me the same thing.

I would like to do things correctly, that of course is what i want.

Fred

What does register post mean?


Message was edited by: fredbell Reeza has provided an example that seems to do what you are mentioning, correct?


Message was edited by: fredbell
fredbell
Fluorite | Level 6
Hi Reeza

How would I convert this to date instead of datetime?

Fred
RickM
Fluorite | Level 6
> How would I convert this to date instead of
> datetime?

Did you try the function Datepart?
fredbell
Fluorite | Level 6
Thanks Rick, that worked.

This thing is driving me crazy.

Fred
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
You really need to get a clear understanding about your external data representation, specifically that you are working with a character-string of data that appears as a date-time. And within SAS, you must convert that string to a SAS numeric DATETIME (or DATE) variable value, so that SAS functions can operate on the resulting numeric value (represented as either a datetime value ( "seconds since 1/1/1960") or a date value ("days since 1/1/1960").

The SAS Language Dictionary and SAS Language Concepts guides each have reference material on this topic, and also the SAS support website has technical / conference papers on the topic.

Until you devote time to self-initiated review and attention, these facilities will continue to course you have suggested.

Scott Barry
SBBWorks, Inc.
Reeza
Super User
See this page when you get a chance, for help on working with data in SAS.

http://www.ats.ucla.edu/stat/sas/modules/default.htm

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 16 replies
  • 12807 views
  • 0 likes
  • 6 in conversation