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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
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;

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User
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;
ballardw
Super User

One clarification, what if A, or your "first" variable, has a missing value? Is that to be an exception to not adding anything else?

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2 replies
  • 923 views
  • 2 likes
  • 3 in conversation