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?

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 486 views
  • 2 likes
  • 3 in conversation