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
;;;;
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 🙂
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 🙂
Glad to help 🙂
Well, starting to browse the communities here, you've come a long way already.
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!
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.