BookmarkSubscribeRSS Feed
AgaWS
Fluorite | Level 6

Hi guys,

 

I want to create a new numeric variable for each participant which is the sum of all menge values from this participant. Each participant has it's own unique Id which is the variable TN_NR and several values for menge. That's why there are several rows for one participant. Here is a part of my dataset:

QA.png

I came this far with the SAS code:

data epic;
set epic;
newmenge = sum (menge);
run;

I am new to programming in SAS and I don't know how to tell SAS to summarize all values of menge for each participant into the new variable.

 

I appreciate every help!! Thanks in advance!!

6 REPLIES 6
Reeza
Super User
******************************************************;
*Add average value to a dataset;
*Solution 1 - PROC MEANS + Data step;
******************************************************;

proc means data=sashelp.class noprint;
    output out=avg_values mean(height)=avg_height;
run;

data class_data;
    set sashelp.class;

    if _n_=1 then
        set avg_values;
run;

proc print data=class;
run;

*Solution 2 - PROC SQL - note the warning in the log;
PROC SQL;
Create table class_sql as
select *, mean(height) as avg_height
from sashelp.class;
quit;

******************************************************;
*Add average value to a dataset - with grouping variables;
*Solution 1 - PROC MEANS + Data step;
******************************************************;
proc means data=sashelp.class noprint nway;
class sex;
    output out=avg_values mean(height)=avg_height;
run;

*sort data before merge;
proc sort data=sashelp.class out=class;
by sex;
run;

data class_data;
 merge class avg_values;
 by sex;


run;

proc print data=class_data;
run;

*Solution 2 - PROC SQL - note the warning in the log;
PROC SQL;
Create table class_sql as
select *, mean(height) as avg_height
from sashelp.class
group by sex;
quit;

@AgaWS wrote:

Hi guys,

 

I want to create a new numeric variable for each participant which is the sum of all menge values from this participant. Each participant has it's own unique Id which is the variable TN_NR and several values for menge. That's why there are several rows for one participant. Here is a part of my dataset:

QA.png

I came this far with the SAS code:

data epic;
set epic;
newmenge = sum (menge);
run;

I am new to programming in SAS and I don't know how to tell SAS to summarize all values of menge for each participant into the new variable.

 

I appreciate every help!! Thanks in advance!!


 

Reeza
Super User
Replace mean with SUM in your use case.
AgaWS
Fluorite | Level 6

Thank you so much for the support!!

PaigeMiller
Diamond | Level 26

Try PROC SUMMARY

 

proc summary data=epic nway;
    class tn_nr;
    var menge;
    output out=want sum=newmenge;
run;

If you want this sum inserted back into your original data set

 

data want2;
    merge epic want(drop= _freq_ _type_);
    by tn_nr;
run;
--
Paige Miller
PaigeMiller
Diamond | Level 26

@AgaWS wrote:

I came this far with the SAS code:

data epic;
set epic;
newmenge = sum (menge);
run;

I am new to programming in SAS and I don't know how to tell SAS to summarize all values of menge for each participant into the new variable.

 

I appreciate every help!! Thanks in advance!!


Hopefully, this advice will help you. The SUM() function in a DATA step, and nearly all functions in a DATA step, work by performing the function across a row of values. If you wanted to sum many variables in a row, the SUM function is what you want.

 

But you want the sum to go down a column, not across a row. That's where you need to use PROCs, which (with a few rare exceptions), perform arithmetic down column(s).

--
Paige Miller
AgaWS
Fluorite | Level 6

Thank you so much for the help!! I could manage it perfectly with your advice.

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
  • 6 replies
  • 2358 views
  • 4 likes
  • 3 in conversation