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

I am trying to create a new variable that will be equal to the value of another variable (i.e. any one of X1 through X10). However, which of the X variables it will be set equal to is stored in a separate variable (Near), where the suffixes are stored.

 

I can actually do this simply with a bunch of if/then data steps. However, since I will probably repeat this for a number of different variables and there are 14 suffixes total, I'd love to find a way to automate it. Here's that simple code, and at the bottom is a sample from the data.

 

Thank you for any help!

 

data aaa1;
set aaa;
if near=1801 then las_trd_near=las_trd_1801;

if near=1802 then las_trd_near=las_trd_1802;

.

.

.

if near=1902 then las_trd_near=las_trd_1902;

run;

 

 


data WORK.AAA;
infile datalines;
input LAS_TRD_1801:BEST12. LAS_TRD_1802:BEST12. LAS_TRD_1901:BEST12. near:32.;
datalines;
15 25 40 1801
75 26 45 1801
90 20 60 1802
50 30 70 1802
45 40 65 1803
;;;;

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

You can do something like this

 

data WORK.AAA;
infile datalines;
input LAS_TRD_1801:BEST12. LAS_TRD_1802:BEST12. LAS_TRD_1901:BEST12. near:32.;
datalines;
15 25 40 1801
75 26 45 1801
90 20 60 1802
50 30 70 1802
45 40 65 1803
;;;;

data aaa1;
   set aaa;
   las_trd_near=input(vvaluex(cats('LAS_TRD_', near)), best12.);
run;

 

@alann, please let me know if this works for you 🙂 

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

You can do something like this

 

data WORK.AAA;
infile datalines;
input LAS_TRD_1801:BEST12. LAS_TRD_1802:BEST12. LAS_TRD_1901:BEST12. near:32.;
datalines;
15 25 40 1801
75 26 45 1801
90 20 60 1802
50 30 70 1802
45 40 65 1803
;;;;

data aaa1;
   set aaa;
   las_trd_near=input(vvaluex(cats('LAS_TRD_', near)), best12.);
run;

 

@alann, please let me know if this works for you 🙂 

alann
Calcite | Level 5
Thank you so much! It works beautifully.
I love the infinite capabilities of SAS, and hope to get more proficient with using all these different functions.
PeterClemmensen
Tourmaline | Level 20

Glad to help 🙂

 

Well, starting to browse the communities here, you've come a long way already.

alann
Calcite | Level 5
Thank you! I've been browsing the communities for a while now, and usually can find the solution I need from past posts, but first time posting, it was much more efficient 🙂

It's just I'd love to find a tutorial that would give me an overview of primary useful functions, so I'd know what to search for when I need to use. For example, I knew of cats function but didn't know I could use it to concatenate a string constant with the numeric value of an observation, that's brilliant! I use concatenate function very effectively in Excel (and that's how I can automate writing tons of line of code in SAS, it just doesn't look good), so it'd be great if I can master these skills in SAS. Hopefully, over time, I guess.
Thank you again!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 1588 views
  • 0 likes
  • 2 in conversation