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

Hello, 

 

Relatively new to SAS Enterprise guide. I am trying to add two columns together into one but would like to add the values if there are no missing values. When there are missing values I would like a 0 in its place. 

 

Example: 

This is what I could like:

 

Column 1  Column 2   New Column  

4                    6                        10

0                    0                         0

1                    0                         1

0                    1                         1

 

I have tried  coalesce(t1.Column 1,t1.Column 2) but this does not add the values. 

 

Thank you in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

You need to have two calculations, a SUM to do the addition and a COALESCE to replace with 0 if missing. 

 

coalesce(sum(t1.Column 1,t1.Column 2), 0) 

@andimo89 wrote:

Hello, 

 

Relatively new to SAS Enterprise guide. I am trying to add two columns together into one but would like to add the values if there are no missing values. When there are missing values I would like a 0 in its place. 

 

Example: 

This is what I could like:

 

Column 1  Column 2   New Column  

4                    6                        10

0                    0                         0

1                    0                         1

0                    1                         1

 

I have tried  coalesce(t1.Column 1,t1.Column 2) but this does not add the values. 

 

Thank you in advance!


 

 

View solution in original post

5 REPLIES 5
CurtisMackWSIPP
Lapis Lazuli | Level 10

You are looking for the SUM function.  It ignores missings.

CurtisMackWSIPP
Lapis Lazuli | Level 10
It look like you are in a SQL statement. There are two SUM statements. With one parameter, it sums across rows. If it has more than one, it sums within the row.
andimo89
Obsidian | Level 7
Hi CurtisMackOFM,

Thanks for the reply. I am currently using the GUI for the advanced expression capability in SAS EG. But row sum might be the way to go.
Reeza
Super User

You need to have two calculations, a SUM to do the addition and a COALESCE to replace with 0 if missing. 

 

coalesce(sum(t1.Column 1,t1.Column 2), 0) 

@andimo89 wrote:

Hello, 

 

Relatively new to SAS Enterprise guide. I am trying to add two columns together into one but would like to add the values if there are no missing values. When there are missing values I would like a 0 in its place. 

 

Example: 

This is what I could like:

 

Column 1  Column 2   New Column  

4                    6                        10

0                    0                         0

1                    0                         1

0                    1                         1

 

I have tried  coalesce(t1.Column 1,t1.Column 2) but this does not add the values. 

 

Thank you in advance!


 

 

andimo89
Obsidian | Level 7

Thank you so much!!! This worked perfectly! I appreciate it. 😊

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 5 replies
  • 7740 views
  • 1 like
  • 3 in conversation