Select to view content in your preferred language

I need help adding an integer to certain records based on a variable

3146
14
Jump to solution
02-13-2013 03:52 AM
LarryGaudieri
Deactivated User
I have a field with times, I need to create a new field (which I've done) and populate it with a "1" if the record meets the criteria of being 9 (minutes) or less. My novice approach tells me to use Calculate Field tool from the Toolbox but I'm having trouble with the Field Calculator in formulating the proper expression.

If it were a couple dozen rows, Id manually input them but I have 50K worth of instances to sort and populate based on that variable (if I'm saying that correctly). Can someone point me in the right direction?

Thanks,
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
LarryGaudieri
Deactivated User
Anthony,

The RESPTIME field is float, the LESSTHAN9 field is text. So:

IF[RESPTIME] <=9 THEN
LESSTHAN9 = "1"
ELSE
LESSTHAN9 = "0"
END IF

I am a little confused, don't I need brackets enclosing the field on the second and fourth line? Also, If I check the Show Codeblock toggle, the LESSTHAN9= shows up above the bottom field; what is required in that lower field? I know your example showed something there but I'm still a little unclear about that.

View solution in original post

0 Kudos
14 Replies
AnthonyGiles
Honored Contributor
larry,

Try something like this in your field calculator:

Tick the Show Codeblock

Code:

If [TimeField] <= 9 Then
  Newfield =  1
Else
  Newfield = 0
End If

And below:
Fieldname =
Newfield

Regards

Anthony
0 Kudos
LarryGaudieri
Deactivated User
Awesome Anthony, much thanks.
0 Kudos
LarryGaudieri
Deactivated User
larry,

Try something like this in your field calculator:

Tick the Show Codeblock

Code:

If [TimeField] <= 9 Then
  Newfield =  1
Else
  Newfield = 0
End If

And below:
Fieldname =
Newfield

Regards

Anthony


Anthony,

I'm missing something. See the attached clip. [ATTACH=CONFIG]21749[/ATTACH]
0 Kudos
LarryGaudieri
Deactivated User
Or, take a look at the calculator dialog. Maybe my error will glare a little brighter....[ATTACH=CONFIG]21750[/ATTACH]
0 Kudos
AnthonyGiles
Honored Contributor
Larry,

Are your resptime and lessthan9 fields set as integer values or string. If they are string put speech marks around your 9, 1 and 0 values

Regards

Anthony
0 Kudos
LarryGaudieri
Deactivated User
Anthony,

The RESPTIME field is float, the LESSTHAN9 field is text. So:

IF[RESPTIME] <=9 THEN
LESSTHAN9 = "1"
ELSE
LESSTHAN9 = "0"
END IF

I am a little confused, don't I need brackets enclosing the field on the second and fourth line? Also, If I check the Show Codeblock toggle, the LESSTHAN9= shows up above the bottom field; what is required in that lower field? I know your example showed something there but I'm still a little unclear about that.
0 Kudos
AnthonyGiles
Honored Contributor
Larry,

no you don't need brackets around the 2 & 4 lines as all you are doing is assigning a value to a variable in this case called lessthan9. In the bottom box your telling your field lessthan9 to be equal to the variable lessthan9 from the code block above.

Hope that make sense

Regards

Anthony
0 Kudos
LarryGaudieri
Deactivated User
Yes it does. And while you were responding I tried to run the calculation again, this time I typed it out all on one single line instead of a structure format like your example and it is now running (still crunching at 79%, as if it working correctly it will be adding a "1" to approximately 30,000 records) so, at least it didn't error out this time. I'll let you know if it worked. Thanks for your patience.

I'm new to expressions and using the Field Calculator and would appreciate any resources (PDF's or websites) for beginners you can point me to....
0 Kudos
AnthonyGiles
Honored Contributor
Larry,

There are examples in the help, here:

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Calculate_Field_examples/005s0000002m0...

Or if you want to know more about vbscript a good place is w3schools:

http://www.w3schools.com/vbscript/vbscript_examples.asp

Regards

Anthony
0 Kudos