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

I have a problem that I hope a friendly soul out there could help me to solve. 

 

I have a data set  like

 

DAY

VALUE

1

1

1

2

1

3

1

6

2

8

2

6

2

4

3

15

3

10

3

35

3

20

3

5


and now I wish to do conditional operations so that for each DATE I create a new variable containing the average VALUE for that DAY, i.e. 

 

DAY

VALUE

Mean_PER_DAY

1

1

3

1

2

3

1

3

3

1

6

3

2

8

6

2

6

6

2

4

6

3

15

17

3

10

17

3

35

17

3

20

17

3

5

17

 

 

This is just a small subset of my dataset that contains thousands of DATE-observations, so using if DATE=2 etc. is not a good solution.


Is there anyone who could write me some example code for this? 

 

 

Thank you in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

You may need to debug my syntax here, but the simplest way would be to use SQL:

 

proc sql noprint;

create table want as

select *, mean(value) as mean_PER_DAY

from have

group by day;

quit;

View solution in original post

3 REPLIES 3
Astounding
PROC Star

You may need to debug my syntax here, but the simplest way would be to use SQL:

 

proc sql noprint;

create table want as

select *, mean(value) as mean_PER_DAY

from have

group by day;

quit;

MiniRadde
Obsidian | Level 7

Thank you so much @Astounding, I tried to do this by several rounds of datasteps and sorting. This really really helöped me a lot and saved me (probably several days) of extra work! Thank you!

Rick_SAS
SAS Super FREQ

You can do this with PROC MEANS followed by a MERGE.  the following assumes that the data are sorted by DAY:

 

proc means data=Have noprint;
by Day;
var Value;
output out=Means mean=Mean_Per_Day;
run;

data Want;
merge Have Means(keep=Day Mean_Per_Day);
by Day;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 1117 views
  • 0 likes
  • 3 in conversation