You can do this with an XPath query.
For example, in a formula, you can reference instance('states')/root/item[name=${stateFieldName}]/code
to get the code of the selected stateFieldName
(I'm assuming that's the name of the select_one
field in the survey.