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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

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;

View solution in original post

1 REPLY 1
PeterClemmensen
Tourmaline | Level 20

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;

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
  • 1 reply
  • 658 views
  • 1 like
  • 2 in conversation