I have the dataset below:
ID | a | b | c | d |
1 | 1 | 1 | . | . |
2 | 1 | . | 1 | 1 |
3 | 1 | 1 | 1 | . |
and I want to add another column called "Result", that sum the variables a,b,c and d without taking into account the missing values and values after the missings.
The data I want to get is like this one:
ID | a | b | c | d | Result |
1 | 1 | 1 | . | . | 2 |
2 | 1 | . | 1 | 1 | 1 |
3 | 1 | 1 | 1 | . | 3 |
Ps: in my real data I have more than 4 variables (a,b,c,d)
How could I program it in SAS and thanks in advance.
data have;
infile datalines dlm="09"x;
input ID a b c d;
datalines;
1 1 1 . .
2 1 . 1 1
3 1 1 1 .
;
data want;
set have;
array x {*} a--d;
do result = 1 to dim(x) until (x{result} = .);
end;
result = result - 1;
run;
data have;
infile datalines dlm="09"x;
input ID a b c d;
datalines;
1 1 1 . .
2 1 . 1 1
3 1 1 1 .
;
data want;
set have;
array x {*} a--d;
do result = 1 to dim(x) until (x{result} = .);
end;
result = result - 1;
run;
One clarification, what if A, or your "first" variable, has a missing value? Is that to be an exception to not adding anything else?
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.