Hello SAS communities!
I am trying to create a new column and the simple example is shown below...
have data:
ID | ColA | ColB | ColC | What_Col |
1 | A1 | B1 | C1 | ColA |
2 | A2 | B2 | C2 | ColB |
3 | A3 | . | C3 | ColA |
4 | . | B4 | C4 | ColC |
Want Data:
ID | ColA | ColB | ColC | What_Col | Value_Col |
1 | A1 | B1 | C1 | ColA | A1 |
2 | A2 | B2 | C2 | ColB | B2 |
3 | A3 | . | C3 | ColA | A3 |
4 | . | B4 | C4 | ColC | C4 |
What_Col is the variable that directs which column to search.
Value_Col is the variable I would like to create that will retrieve the value from the column What_Col specifies.
I tried to write a SAS code as below but it does not work.
proc sql noprint;
select What_col into col_list separated by ',';
from have;
data want;
set have;
count+1;
do i=1 to 4;
Value_col=resolve(scan("col_list",i,',');
end;
run;
Any recommendations or advices are welcome!
Thanks in advance.
A job for the Vvaluex Function
data have;
input ID ColA $ ColB $ ColC $ What_Col $;
datalines;
1 A1 B1 C1 ColA
2 A2 B2 C2 ColB
3 A3 . C3 ColA
4 . B4 C4 ColC
;
data want;
set have;
Value_Col = vvaluex(What_Col);
run;
A job for the Vvaluex Function
data have;
input ID ColA $ ColB $ ColC $ What_Col $;
datalines;
1 A1 B1 C1 ColA
2 A2 B2 C2 ColB
3 A3 . C3 ColA
4 . B4 C4 ColC
;
data want;
set have;
Value_Col = vvaluex(What_Col);
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.