Help using Base SAS procedures

Function Datepart

Reply
Contributor
Posts: 73

Function Datepart

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
PROC Star
Posts: 7,363

Re: Function Datepart

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
Super Contributor
Posts: 281

Re: Function Datepart

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.
Contributor
Posts: 73

Re: Function Datepart

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
Super Contributor
Super Contributor
Posts: 3,174

Re: Function Datepart

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.
Contributor
Posts: 73

Re: Function Datepart

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
Super User
Posts: 17,887

Re: Function Datepart

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.
Contributor
Posts: 73

Re: Function Datepart

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
Super Contributor
Super Contributor
Posts: 3,174

Re: Function Datepart

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
Contributor
Posts: 73

Re: Function Datepart

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
Contributor
Posts: 73

Re: Function Datepart

Hi Reeza

How would I convert this to date instead of datetime?

Fred
Regular Contributor
Posts: 165

Re: Function Datepart

> How would I convert this to date instead of
> datetime?

Did you try the function Datepart?
Contributor
Posts: 73

Re: Function Datepart

Thanks Rick, that worked.

This thing is driving me crazy.

Fred
Super Contributor
Super Contributor
Posts: 3,174

Re: Function Datepart

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.
Super User
Posts: 17,887

Re: Function Datepart

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
Ask a Question
Discussion stats
  • 16 replies
  • 8000 views
  • 0 likes
  • 6 in conversation