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.
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;
Can you post what the output would look like?
Hi Reeza
If I am summing the variables, the output should look like:
Obs | x1 | x2 | x3 | b | c | d |
1 | 1 | 2 | 3 | x1 | x2 | 3 |
2 | 2 | 3 | 5 | x2 | x3 | 8 |
3 | 5 | 3 | 6 | x1 | x3 | 14 |
so here, we have a variable d. D sums the x1--x2 in first observation. x2--x3 in second observation and so on.
Thnaks
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;
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:
Obs | x1 | x2 | x3 | b | c | flag | i | d |
1 | 1 | 2 | 3 | x1 | x2 | 0 | 4 | . |
2 | 2 | 3 | 5 | x2 | x3 | 0 | 4 | . |
3 | 5 | 3 | 6 | x1 | x3 | 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
Yup the note tells you whats wrong.
Sorry Reeza
I am not able to solve this problem. Can you please highlight what is wrong with the code now? i am clueless
Thanks
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;
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
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
Thanks Mike. This also worked like charm and quite neat.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.