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?
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.
Ready to level-up your skills? Choose your own adventure.