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?
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.