This article describes how to bring a variable over from one linked data set to another. This is useful when you wish to make calculations (all R-based calculations and variables) that require all your variables to be in the same data set.
Requirements
- A document with two data sets loaded in the Data Sources.
- A Relationship between the files. See How to Create Data File Relationships for details.
- Note, this approach can only be used in a One to One or One to Many relationship where you are copying data from a data set that represents the One part of the relationship. It cannot be used with a Many to Many relationship which is instead performing Data Fusion.
Method
1. Select the Data Set in the Data Sources where you wish to add the variable. In this example, our data set is called File1 and we are bringing a variable over from File2.
2. In the Data Sources tree, hover over any variable and click the Plus (+), then select Custom Code > JavaScript - Numeric or JavaScript - Text, depending on the format of the variable you wish to copy.
Note that Nominal, Ordinal, or Date/Time variables should be set up as a numeric JavaScript variable.
3. Select the variable in the second Data Set and drag it over to the JavaScript code editor at the top of the screen.
You can also enter this manually using the below format:
Q.GetValue('source_variable_name', 'source_data_set')
The source_variable_name will match General > Name, and the source_data_set in this case would be File2.
Note, if the variable being added to your data set represents the Many in a One to Many relationship, i.e. has multiple matching records, you will need to indicate which matching record to map it to. For example, appending a [0]
to the end of the code will take the first matching record:
Q.GetValue('source_variable_name', 'source_data_set')[0]
4. Change Data > Structure to the correct data structure.
5. OPTIONAL: If you are copying over a categorical variable such as Nominal or Ordinal, it will only carry over the values. You can, however, copy over the labels by doing the following:
- To copy the labels, select the source variable in the Data Sources tree and go to Data > Data Values > Labels > Export > CTRL + C.
- To then apply this to your new variable, select it in the Data Sources tree and go to Data > Data Values > Labels > Import > CTRL + V > OK.