Hi,
So I have the dataset below and want to create a new variable (Weight_to_use).
If year = 2017 then weight_to_use should be populated from Weight_17.
If year = 2018, then weight_to_use should be 135 from Weight_18.
etc. etc.
I'm not sure how to right this logic. Any ideas?
Year | ID | Weight_17 | Weight_18 | Weight_19 | Weight_to_use |
2017 | 1234 | 130 | 135 | 132 | |
2018 | 3344 | 234 | 250 | 244 | |
2019 | 9999 | 180 | 192 | 167 |
@Etoo12121 wrote:
Hi,
So I have the dataset below and want to create a new variable (Weight_to_use).
If year = 2017 then weight_to_use should be populated from Weight_17.
If year = 2018, then weight_to_use should be 135 from Weight_18.
etc. etc.
I'm not sure how to right this logic. Any ideas?
Year ID Weight_17 Weight_18 Weight_19 Weight_to_use 2017 1234 130 135 132 2018 3344 234 250 244 2019 9999 180 192 167
data have; input Year ID $ Weight_17 Weight_18 Weight_19; datalines; 2017 1234 130 135 132 2018 3344 234 250 244 2019 9999 180 192 167 ; data want; set have; array w (2017:2019) Weight_17 Weight_18 Weight_19; weighttouse=w[year]; run;
Please note the data step to provide an actual data set to work with.
The Array w uses the (lowerbound:upperbound) notation to describe the values used to access elements of the array. In this case 2017 corresponds to weight_17, 2018 to Weight_18, and 2019 to weight_19.
Then you use the year value in that define range to access the specific value from the array.
@Etoo12121 wrote:
Hi,
So I have the dataset below and want to create a new variable (Weight_to_use).
If year = 2017 then weight_to_use should be populated from Weight_17.
If year = 2018, then weight_to_use should be 135 from Weight_18.
etc. etc.
I'm not sure how to right this logic. Any ideas?
Year ID Weight_17 Weight_18 Weight_19 Weight_to_use 2017 1234 130 135 132 2018 3344 234 250 244 2019 9999 180 192 167
data have; input Year ID $ Weight_17 Weight_18 Weight_19; datalines; 2017 1234 130 135 132 2018 3344 234 250 244 2019 9999 180 192 167 ; data want; set have; array w (2017:2019) Weight_17 Weight_18 Weight_19; weighttouse=w[year]; run;
Please note the data step to provide an actual data set to work with.
The Array w uses the (lowerbound:upperbound) notation to describe the values used to access elements of the array. In this case 2017 corresponds to weight_17, 2018 to Weight_18, and 2019 to weight_19.
Then you use the year value in that define range to access the specific value from the array.
@ballardw has definitely provided the correct solution but you can also look into VVALUEX() if you'd like. It returns the value in a column, if you pass it the name of the column in question.
weight_to_use = vvaluex(catt('weight_', substrn(year, 3, 2)));
@Etoo12121 wrote:
Hi,
So I have the dataset below and want to create a new variable (Weight_to_use).
If year = 2017 then weight_to_use should be populated from Weight_17.
If year = 2018, then weight_to_use should be 135 from Weight_18.
etc. etc.
I'm not sure how to right this logic. Any ideas?
Year ID Weight_17 Weight_18 Weight_19 Weight_to_use 2017 1234 130 135 132 2018 3344 234 250 244 2019 9999 180 192 167
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.