Error in if (req_dur > maxdur) { : missing value where TRUE/FALSE needed - while-loop

I am trying to call water level data from NOAA using the rnoaa library. Water level data is restricted to 31 days and I need data across multiple months. I found the following 'while' loop when looking for similar questions: Looking for a solution in R to loop through a date range to get climate data from NOAA API
When I run the code below, I get the following error:
Error in if (req_dur > maxdur) { : missing value where TRUE/FALSE needed
beginDate <- as.Date('2022-01-01')
endDate <- as.Date('2022-05-01')
date <- beginDate
while (date <= endDate){
water_level <- coops_search(
station_name = 8418150,
begin_date = date,
end_date = date + 1,
product = 'water_level',
datum = 'MHW',
units = 'metric',
time_zone = 'lst')
date <- date + 1
}
I've tried changing the beginDate and endDate parameters to the following and still get the same error:
beginDate<- as.Date("20220101",format = "%Y%m%d")
endDate <- as.Date("20220501",format = "%Y%m%d" )
Any suggestions? Thank you!

Related

PROC compiler fails for connect with prior in case using 'NOCYCLE'

I am very new to PROC and I was compiling following query in .pc file,
`
SELECT * FROM CUST
WHERE CUSTOMER_ID = :sqli_customer_id
START WITH SUBSCRIBER_NO = :sqli_subscriber_no
CONNECT BY NOCYCLE PRIOR PRV_CTN = SUBSCRIBER_NO
ORDER BY ROWNUM DESC
`
following query fails to compile in PROC complier
but when I remove the NOCYCLE from query, file gets successfully compiled.
I get following error with NOCYCLE:
PCC-S-02201, Encountered the symbol "PRIOR" when expecting one of the following:
= ( * < > + - / . # ^= | != <= >= <> at, not, between, in, is, like, day, hour, minute, month, second, year,
The symbol "*" was substituted for "PRIOR" to continue.
`
Any suggestion will be helpful.
Thanks,
Nitin T.

Need to convert a numeric field to date in this query

I am running this in sql server but there is an error on the date compare as the column cadodt is a numeric. Is there a quick and easy way to convert that col to numeric? It's date is in YYYYMMDD but I cannot compare against a date field like current date.
select * from openquery(IBSIBM, 'select CABANO, CABCRC, CABCTR, CABCUR, CABKCO, CACLVL, CACOTI,
CACRDT, CACTTP, CADODT, CADOTY, CAEXRT, CAIDNO, CAJONO, CANCRX,
CAPERI, CAREFX, CARSCD, CASCRC, CASCTR, CASTAT, CASTMT, CASYID,
CATIML, CATREF, CATTXT, CATYPC, CAUSER, CAVADT, CAVODT, CAVONO,
CAVOTY ,NANAME from CA1665AFCV.SROCBA,
CA1665AFCV.srolta,
CA1665AFCV.sronam
WHERE CADOTY = ''CHK'' and cajono=ctjono and cadodt = CURRENT DATE()
and caperi=ctperi and caidno=ctidno and ctveno=nanum
and CASTAT = ''Y''')
Assuming cadodt is actually an integer or numeric field and not a character string, try this:
select * from openquery(IBSIBM,
'select CABANO, CABCRC, CABCTR, CABCUR, CABKCO, CACLVL, CACOTI,
CACRDT, CACTTP, CADODT, CADOTY, CAEXRT, CAIDNO, CAJONO, CANCRX,
CAPERI, CAREFX, CARSCD, CASCRC, CASCTR, CASTAT, CASTMT, CASYID,
CATIML, CATREF, CATTXT, CATYPC, CAUSER, CAVADT, CAVODT, CAVONO,
CAVOTY, NANAME
from CA1665AFCV.SROCBA, CA1665AFCV.srolta, CA1665AFCV.sronam
WHERE CADOTY = ''CHK''
and cajono = ctjono
and cadodt = YEAR(CURRENT DATE) * 10000 + MONTH(CURRENT DATE) * 100 + DAY(CURRENT DATE)
and caperi = ctperi
and caidno = ctidno
and ctveno = nanum
and CASTAT = ''Y''')
Aside: I would strongly recommend against using comma joins. The longer ANSI 92 join syntax is much easier to read and maintain. I'd also strongly recommend using qualified column names. This schema is not the easiest to follow as it is. No need to make it even more arcane.
You have these options
convert cadodt from string to date
convert cadodt from intger to date
convert current date to YYYYMMDD string
convert current date to YYYYMMDD integer
1 and 3 assume cadodt is a string
2 and 4 assume cadodt is an intger
3 and 4 are the more efficient approaches as you only change one value.
e.g.
(3) if cadodt is a string in YYYMMDD format:
AND cadodt = VARCHAR_FORMAT(CURRENT DATE, 'YYYYMMDD')
(4) if cadodt is an integer representing YYYYMMDD
AND cadodt = YEAR(CURRENT DATE) * 10000 + MONTH(CURRENT DATE) * 100 + DAY(CURRENT DATE)
the CURRENT DATE() may be causing you some issues use CURRENT_DATE()
Try this:
select * from openquery(IBSIBM, 'select CABANO, CABCRC, CABCTR, CABCUR, CABKCO, CACLVL, CACOTI,
CACRDT, CACTTP, CADODT, CADOTY, CAEXRT, CAIDNO, CAJONO, CANCRX,
CAPERI, CAREFX, CARSCD, CASCRC, CASCTR, CASTAT, CASTMT, CASYID,
CATIML, CATREF, CATTXT, CATYPC, CAUSER, CAVADT, CAVODT, CAVONO,
CAVOTY ,NANAME from CA1665AFCV.SROCBA,
CA1665AFCV.srolta,
CA1665AFCV.sronam
WHERE CADOTY = ''CHK'' and cajono=ctjono and cadodt = CURRENT_DATE()
and caperi=ctperi and caidno=ctidno and ctveno=nanum
and CASTAT = ''Y''')
Replace CADODT = CURRENT DATE() with:
date(substr(char(CADODT), 1, 4) || '-'|| substr(char(CADODT),5, 2) || '-'|| substr(char(CADODT), 7, 2)) = CURRENT DATE
That should convert it from an integer to a date.

Create new Character variables from Date variable in sas

I have the following data set:
Date
May2005
May2005
May2005
June2005
.
.
.
May2006
May2006
May2006
.
.
.
May2007
May2007
May2007
I am trying to create three new variables such that Date05 = May2005 when Date1 = May2005, Date06 = May2006 when Date1 = May2006, and so on.
I thought of the following code, but it doesn't work:
data new;
set afinaldelaware;
if (Date1 EQ '01May2005'd or Date1 EQ '01May2006'd or Date1 EQ '01May2007'd)
then do;
Date05 = '01May2005';
Date06 = "01May2006';
Date07 = 'May2007';
end;
run;
Since your dates are formatted as mmyyy7. you don't know the exact date which is likely why your comparison fails. You can compare the month and year using the respective functions instead of date literals. Note that you're creating character variables not SAS date variables, which may be what you intend.
data new;
set afinaldelaware;
if (month(date1)=5 and year(date1) in (2005 2006 2007))
then do;
Date05 = '01May2005';
Date06 = "01May2006';
Date07 = 'May2007';
end;
run;

DTP routine to find timestamp range

I am working on a DTP filter routine for a timestamp field in the format of YYYYMMDDhhmmss. I am trying to declare the range as ({timestamp 3 months ago} to {current timestamp}). I am very new to ABAP and basically have the code set up right now so it doesn't have any syntax errors. I am currently having trouble getting the correct timestamp to be filled when I assign it to "l_ts".
*$*$ begin of routine - insert your code only below this line *-*
BREAK-POINT.
DATA: l_idx LIKE sy-tabix,
l_ts TYPE rstimestmp,
l_ts2 TYPE rstimestmp.
READ TABLE l_t_range WITH KEY
fieldname = 'TIMESTAMP'.
l_idx = sy-tabix.
* Capture the Current Date
l_ts = sy-datlo + sy-timlo.
l_ts2 = ( sy-datlo + sy-timlo ) - 93.
IF l_idx <> 0.
* fill the Selection table.
l_t_range-low = l_ts.
l_t_range-sign = 'I'.
l_t_range-option = 'BT'.
l_t_range-high = l_ts2.
MODIFY l_t_range INDEX l_idx.
ELSE.
* fill the Selection table.
l_t_range-fieldname = 'TIMESTAMP'.
l_t_range-low = l_ts.
l_t_range-high = l_ts2.
l_t_range-sign = 'I'.
l_t_range-option = 'BT'.
APPEND l_t_range.
ENDIF.
p_subrc = 0.
*$*$ end of routine - insert your code only before this line *-*
You're mixing up timestamps and discrete date and time calculations - won't work this way. What you'll really want is probably something like this:
DATA: l_date TYPE d,
l_time TYPE t,
l_ts TYPE rstimestmp.
FIELD-SYMBOLS: <ls_param> LIKE LINE OF l_t_range.
* ensure that the parameter exists
READ TABLE l_t_range ASSIGNING <ls_param> WITH KEY fieldname = 'TIMESTAMP'.
IF sy-subrc <> 0.
APPEND INITIAL LINE TO l_t_range ASSIGNING <ls_param>.
<ls_param>-fieldname = 'TIMESTAMP'.
ENDIF.
<ls_param>-sign = 'I'.
<ls_param>-option = 'BT'.
* "from" date = three months ago, more or less - probably the start of the day?
l_date = sy-datlo - 93.
l_time = '000000'. " or sy-timlo.
CONVERT DATE l_date TIME l_time INTO TIME STAMP l_ts TIME ZONE sy-zonlo.
<ls_param>-low = l_ts.
* "to" date = today - probably the end of the day?
l_date = sy-datlo.
l_time = '235959'. " or sy-timlo.
CONVERT DATE l_date TIME l_time INTO TIME STAMP l_ts TIME ZONE sy-zonlo.
<ls_param>-high = l_ts.

Rails Unit Test

Please help. I am trying to write a unit test for a method in my model. It keeps giving me the following error:
{ 1) Error:
test_Total_for_a_given_date_range_for_Campaigns(LetterTest):
TypeError: can't convert ActiveSupport::Duration into String
c:/Users/Kay/rails-app/CS5942/app/models/letter.rb:19:in +'
c:/Users/Kay/rails-app/CS5942/app/models/letter.rb:19:intribe_total_on'
test/unit/letter_test.rb:17:in `block in '}
This is what i have in my test file:
test "Total for a given date range for Campaigns" do
end_date = (Date.today + 1.day).to_date.strftime('%Y-%m-%d')
expected = Letter.where("letter_campaign_id = '1' and created_at between '#{ (Date.today - 1.day).to_date.strftime('%Y-%m-%d')}' and #{end_date}").count
data = Letter.tribe_total_on( 1 , (Date.today - 1.day).to_date.strftime('%Y-%m-%d') , (Date.today).to_date.strftime('%Y-%m-%d'))
assert_equal expected, data
end
It seems to have a problem with the plus sign. I would be very happy if i could find a solution to it. Thanks.
This is the tribe_total_on method:
def self.tribe_total_on(tribe, start_date, end_date)
#Letter.connection.execute("SELECT COUNT(id) FROM letters WHERE letter_campaign_id = #{tribe} AND created_at BETWEEN '#{start_date.to_date.strftime('%Y-%m-%d')}' and '#{end_date.to_date.strftime('%Y-%m-%d')}'")[0][0]
end_date = end_date + 1.day #This is line 19 ...Had to include the one day to accomodate dates like 2011-06-05 01:02:03
Letter.where("letter_campaign_id = #{tribe} AND created_at between ? and ?", start_date.to_date, end_date.to_date).count
end
Open up Rails console and type in what you're trying to do and you'll get this:
(Date.today).to_date.strftime('%Y-%m-%d') + 1.day
TypeError: can't convert ActiveSupport::Duration into String
from (irb):20:in `+'
You're either going to have to do the conversion to strftime before you pass it to tribe_total_on or you're going to have to add 1.day to the date before you call strftime before you pass it to tribe_total_on.
So:
(Date.today + 1.day).to_date.strftime('%Y-%m-%d')
or:
def self.tribe_total_on(tribe, start_date, end_date)
end_date = (end_date + 1.day).to_date.strftime('%Y-%m-%d')