BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Etoo12121
Obsidian | Level 7

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?

 

YearIDWeight_17Weight_18Weight_19Weight_to_use
20171234130135132 
20183344234250244 
20199999180192167 
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@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.

 

View solution in original post

4 REPLIES 4
ballardw
Super User

@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
Obsidian | Level 7
Thank you @ballardw It got me exactly what I wanted.
Reeza
Super User

@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  

 

Etoo12121
Obsidian | Level 7
Thank you @Reeza. I tried this and it worked like a charm. Fairly simple and to the point. Definitely learned something new today.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 978 views
  • 5 likes
  • 3 in conversation