DATA Step, Macro, Functions and more

Refer variable value as variable name and assign the value?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 86
Accepted Solution

Refer variable value as variable name and assign the value?

Hello

My objective is to refer a variable value to variable name and assigns a value corrosponds to that variable name. Suppose , I have a dataset.

                               Obs    x1    x2    x3    b     c

                                  1      1     2     3    x1    x2

                                  2      2     3     5    x2    x3

                                  3      5     3     6    x1    x3

I would like to create a new variable called D. In this variable, If, I call variable name B, it will give value as x1. What I am looking for is value 1 since x1 variable value for that observation is 1.

Similarly , for second observation, it should call 3 since b value is x2 which refer to 3.

If this is possible, I would furthur like to use this in the Sum(of..) function to sum the values for example: for 1st observation: x1--x2  gives 3, second observation: 8, third observation: 14.

Thanks a lot for all the help.


Accepted Solutions
Solution
‎09-08-2012 03:02 AM
Super User
Posts: 19,855

Re: Refer variable value as variable name and assign the value?

d is missing, set it to 0 at the beginning.

missing value + something is missing in SAS.  

data new1;

set new;

flag=0;

d=0;

array x(3) x1-x3;

do i=1 to 3;

if vname(x(i))=b then flag=1;

else if vname(x(i))=c then flag=0;

if flag=1 then d=d+x(i);

end;

run;

View solution in original post


All Replies
Super User
Posts: 19,855

Re: Refer variable value as variable name and assign the value?

Can you post what the output would look like?

Frequent Contributor
Posts: 86

Re: Refer variable value as variable name and assign the value?

Hi Reeza

If I am summing the variables, the output should look like:

Obsx1x2x3b c d
  1  1 2 3x1x23
  2  2 3 5x2x38
  3  5 3 6x1x314

so here, we have a variable d. D sums the x1--x2 in first observation. x2--x3 in second observation and so on.

Thnaks

Super User
Posts: 19,855

Re: Refer variable value as variable name and assign the value?

you can probably do it using vname function and an array.

loop through and start adding when you the name matches b and end when it matches c.

flag=0;

array x(3) x1-x3;

do i=1 to 3;

if vname(x(i))=b then flag=1;

else if vname(x(i))=c then flag=0;

if flag=1 then d=d+x(i);

end;

run;

Frequent Contributor
Posts: 86

Re: Refer variable value as variable name and assign the value?

Sorry. But this did not work. In SAS, the output is missing values. What I wrote is:

data new;

input x1 x2 x3  b $ c $ ;

datalines;

1 2 3 x1 x2

2 3 5 x2 x3   

5 3 6 x1 x3   

;

proc print;run;

data new1;

set new;

flag=0;

array x(3) x1-x3;

do i=1 to 3;

if vname(x(i))=b then flag=1;

else if vname(x(i))=c then flag=0;

if flag=1 then d=d+x(i);

end;

run;

proc print;run;

The output is:

Obsx1x2x3b c flagid
1 2 3x1x2  0 4.
2 3 5x2x3  0 4.
5 3 6x1x3  0 4.

In the log, it gives the note as follows:

NOTE: Missing values were generated as a result of performing an operation on missing values.

      Each place is given by: (Number of times) at (Line)Smiley SadColumn).

      4 at 167:19

NOTE: There were 3 observations read from the data set WORK.NEW.

NOTE: The data set WORK.NEW1 has 3 observations and 8 variables.

NOTE: DATA statement used (Total process time):

      real time           0.01 seconds

      cpu time            0.01 seconds

Super User
Posts: 19,855

Re: Refer variable value as variable name and assign the value?

Yup the note tells you whats wrong.

Frequent Contributor
Posts: 86

Re: Refer variable value as variable name and assign the value?

Sorry Reeza

I am not able to solve this problem. Can you please highlight what is wrong with the code now? i am clueless Smiley Sad

Thanks

Solution
‎09-08-2012 03:02 AM
Super User
Posts: 19,855

Re: Refer variable value as variable name and assign the value?

d is missing, set it to 0 at the beginning.

missing value + something is missing in SAS.  

data new1;

set new;

flag=0;

d=0;

array x(3) x1-x3;

do i=1 to 3;

if vname(x(i))=b then flag=1;

else if vname(x(i))=c then flag=0;

if flag=1 then d=d+x(i);

end;

run;

Frequent Contributor
Posts: 86

Re: Refer variable value as variable name and assign the value?

Thanks a lot Reeza. It worked like charm now. In the second condition, i also marked the flag=1 then only, I could add to the total.

Thank you so much again for helping.

Regards

Valued Guide
Posts: 765

Re: Refer variable value as variable name and assign the value?

hi ... you have a solution, but here's another another idea ...

data x;

input x1-x3 (b c) (:$2.);

datalines;

1 2 3 x1 x2

2 3 5 x2 x3

5 3 6 x1 x3

;

data y;

set x;

array x(3);

do _n_=input(char(b,2),1.) to input(char(c,2),1.);

   d = sum(d, x(_n_));

end;

run;

x1    x2    x3    b     c      d

1     2     3    x1    x2     3

2     3     5    x2    x3     8

5     3     6    x1    x3    14

Frequent Contributor
Posts: 86

Re: Refer variable value as variable name and assign the value?

Thanks Mike. This also worked like charm and quite neat.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 368 views
  • 3 likes
  • 3 in conversation