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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

10 REPLIES 10
Reeza
Super User

Can you post what the output would look like?

bnarang
Calcite | Level 5

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

Reeza
Super User

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;

bnarang
Calcite | Level 5

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):(Column).

      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

Reeza
Super User

Yup the note tells you whats wrong.

bnarang
Calcite | Level 5

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

Reeza
Super User

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;

bnarang
Calcite | Level 5

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

MikeZdeb
Rhodochrosite | Level 12

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

bnarang
Calcite | Level 5

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

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
  • 10 replies
  • 3360 views
  • 3 likes
  • 3 in conversation