DATA Step, Macro, Functions and more

Value from Coloumn Name

Occasional Contributor
Posts: 15

Value from Coloumn Name


I have a dataset(name : new) which has 4 coloumns(c1 , c2 , c3 ,c4).
c4 coloumn consists the name of the coloumns ( c2,c1,.......).
please see example below:

Dataset: New
c1 c2 c3 c4
1 2 3 c2
1 3 5 c1

I did as follow:
data a;
set new;
t1 = max(0,c1 - c4);

Here I want C4 to be replaced with the value. Meaning in the first row , the value for c4 is c2 so I want value of c2(Value of C2 : 2) to be substituted and so on...

Please help me .

Super Contributor
Super Contributor
Posts: 3,174

Re: Value from Coloumn Name

Have a look at the VVALUE / VVALUEX function.

Scott Barry
SBBWorks, Inc.

Suggested Google advanced search argument, this topic / post:

return value of variable Message was edited by: sbb
Super Contributor
Super Contributor
Posts: 365

Re: Value from Coloumn Name

Hello SASAcc,

I am not sure that understand you correctly. What do you mean by t1 = max(0,c1 - c4);?

Is this t1 = max(0,c1,c2,c3);
or t1 = max(0,c1-c3):?

Using c4 is impossible because c1, c2, c3 are numeric and c4 is character.

Anyway this my solution:
Data New;
input c1 c2 c3 c4 $;
1 2 3 c2
1 3 5 c1
data a;
set new;
t1 = max(0, c1,c2,c3);
if c4="c2" then c2=t1;
else if c4="c1" then c1=t1;
drop t1;
Valued Guide
Posts: 2,175

Re: Value from Coloumn Name

please explain why C4 is to be considered for the MAX()
The value it holds seems to be a pointer to one of the other C(n) columns - so including the value to which C4 points will not introduce anything larger than MAX( of C1-C3 )
Ask a Question
Discussion stats
  • 3 replies
  • 4 in conversation