referring to a field indirectly

Discussions about editing and cleaning data
Post Reply
AriSilva
Posts: 470
Joined: July 22nd, 2016, 3:55 pm

referring to a field indirectly

Post by AriSilva » June 9th, 2020, 4:52 pm

Is it possible to change the contents of a variable without knowing its name?
For example I want to change the contents of a variable named SEX.
So I can write
SEX = 2;

Can I write something like:
string varname = "sex";
setContents(varname, 2);

which would do the same as the direct assigning.
Why would I need that?
I have a need to edit some variables according to some user commands. So I thought about having an excel file with three columns:
1. Id
2. variable
3. new_value
Reading this file and for each line in the file apply the change accordingly.
Best
Ari

Gregory Martin
Posts: 1418
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: referring to a field indirectly

Post by Gregory Martin » June 9th, 2020, 5:23 pm

You can use setvalue: https://www.csprousers.org/help/CSPro/s ... ction.html
setvalue("SEX", 2);
I've used this to do recoding from an Excel file, which sounds like your exact use case.

AriSilva
Posts: 470
Joined: July 22nd, 2016, 3:55 pm

Re: referring to a field indirectly

Post by AriSilva » June 10th, 2020, 10:02 am

Great!
That is exactly what I have in mind!
Best
Ari

Post Reply