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

Hello, what's the best way to insert the sum of a column in each row of a separate variable?

 

Consider a data set

 

DATA data1;
INPUT var1;
DATALINES;
1
2
3;
RUN;

I would like to generate a new variable var2 in that dataset that has the value 6 in each row.

 

I know that I can calculate the sum with PROC MEANS, then extract the value with CALL SYMPUT in another data step and then insert the value in a second data step. I could also think of some SQL join to insert the value but since this is a rather standard procedure I guess that there's an easy way and I just can't come up with it.

1 ACCEPTED SOLUTION
2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

Here is a pure data step approach

 

DATA data1;
INPUT var1;
DATALINES;
1
2
3
;

data want;
    do until (lr1);
        set data1 end=lr1;
        sum+var1;
    end;
    do until (lr2);
        set data1 end=lr2;
        output;
    end;
run;

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
  • 872 views
  • 1 like
  • 3 in conversation