Help using Base SAS procedures

Creating a new variable from multiple observation.

Reply
Occasional Contributor
Posts: 5

Creating a new variable from multiple observation.

Hello,

 

I have a data set that is composed of participants and sedentary time. I want to create a new varible that is the average sedentary time for each participant.  Meaning that I only want one sedtime for each participant.

 

I need a new variable because it want to then regress the average sedentary time with other predictors variables.

 

I am running SAS on SAS studio and I am a beginnier. Any help would be welcome. Thanks

 

example data.

 

participant   Sedtime

1                     4

1                     3

1                     2

2                     1

2                      5

2                      3

3                      4

3                      5

3                      4

 

PROC Star
Posts: 1,809

Re: Creating a new variable from multiple observation.

[ Edited ]
Posted in reply to ebonfil89

do you mean -

proc sql;
create table want as
select participant, mean(sedtime) as avg
from have
group by participant;
quit;
PROC Star
Posts: 1,809

Re: Creating a new variable from multiple observation.

Posted in reply to novinosrin
proc summary data=have nway;
class participant;
var sedtime;
output out=want(drop=_:) mean=/autoname;
run;
Occasional Contributor
Posts: 5

Re: Creating a new variable from multiple observation.

Posted in reply to novinosrin

Thank you for the information. In this procedure, what is the new variable that was created? 

PROC Star
Posts: 1,809

Re: Creating a new variable from multiple observation.

Posted in reply to ebonfil89

Please test the demo that we share so you will have a clear understanding:

data have;
input participant   Sedtime;
datalines;
1                     4
1                     3
1                     2
2                     1
2                      5
2                      3
3                      4
3                      5
3                      4
;

proc summary data=have nway;
class participant;
var sedtime;
output out=want(drop=_:) mean=/autoname;
run;

Results:

participantSedtime_Mean
13
23
34.333333333
Occasional Contributor
Posts: 5

Re: Creating a new variable from multiple observation.

Posted in reply to novinosrin
Thank you so much, option two worked for me.

Proc means data = WORK.IMPORT noprint nway;
class PQ_Study_ID_Unique;
output out = avg_values mean (sed) = ave_sed;
run;

Proc sort data = work.import out = sort;
by PQ_Study_ID_Unique;
run;

data sort_sed;
merge sort avg_values;
run;

proc print data = sort_sed;
run;
Super User
Posts: 23,727

Re: Creating a new variable from multiple observation.

Posted in reply to ebonfil89

Your MERGE doesn’t have a BY statement. I would expect to see one if you’re doing group summaries. Are you sure you’re getting what you expect?

Occasional Contributor
Posts: 5

Re: Creating a new variable from multiple observation.

Hello, I have added the "by" statement. I think that the data was already sorted by ID, so it did give me the same answers. Thank you for your feedback. Very much appreciated it.
Super User
Posts: 23,727

Re: Creating a new variable from multiple observation.

Posted in reply to ebonfil89

ebonfil89 wrote:
Hello, I have added the "by" statement. I think that the data was already sorted by ID, so it did give me the same answers. Thank you for your feedback. Very much appreciated it.

 

If that was the case it means you have a single ID for each record which means there's no point to doing a PROC MEANS. I would be very seriously double checking your results. Something isn't aligning between what you're stating and your code. 

 

Super User
Posts: 23,727

Re: Creating a new variable from multiple observation.

Posted in reply to ebonfil89
Ask a Question
Discussion stats
  • 9 replies
  • 268 views
  • 1 like
  • 3 in conversation