BookmarkSubscribeRSS Feed
Ken_oy
Fluorite | Level 6
Using the following code:
-------------------------------------------
-------------------------------------------
data have;
do group = 'a','b';
do value = 1,3,3,2,5,1,8;
output; end; end; run;

proc sql;
create view numbered as select *,
monotonic() as obsnum from have;
create view want as select numbered.group, numbered.value, cross.value as lookahead from numbered left join numbered as cross
on numbered.group=cross.group and numbered.obsnum+1=cross.obsnum
order by numbered.obsnum;
quit;
-------------------------------------------
-------------------------------------------



We can have the Result below:
-------------------------------------------
-------------------------------------------
Obs group value lookahead

1 a 1 3
2 a 3 3
3 a 3 2
4 a 2 5
5 a 5 1
6 a 1 8
7 a 8 .
8 b 1 3
9 b 3 3
10 b 3 2
11 b 2 5
12 b 5 1
13 b 1 8
14 b 8 .
-------------------------------------------
-------------------------------------------

Question:
How can I calculate the Maximum(Value), by group?
5 REPLIES 5
Flip
Fluorite | Level 6
Max(Value) as maxval, ...
group by group
Ken_oy
Fluorite | Level 6
Hi, But where can I put this statement? Thanks!
Flip
Fluorite | Level 6
This should work. You may have a conflict between order by and group by using different cols.

proc sql;
create view numbered as select *,
monotonic() as obsnum from have;
create view want as select numbered.group, numbered.value, MAX(NUMBERED.VALUE) AS MAXVAL, cross.value as lookahead from numbered left join numbered as cross
on numbered.group=cross.group and numbered.obsnum+1=cross.obsnum
GROUP BY NUMBERED.GROUP
order by numbered.obsnum;
quit;
Ken_oy
Fluorite | Level 6
Thank Flip!!
Thanks for your great help!!

Fixed!!
Ken_oy
Fluorite | Level 6
proc sql;
create view numbered as select *,
monotonic() as obsnum from have;
create view want as select numbered.group, numbered.value, cross.value as lookahead

Max(Value) as maxval,



from numbered left join numbered as cross
on numbered.group=cross.group and numbered.obsnum+1=cross.obsnum

group by group

order by numbered.obsnum;
quit;


---------------------------------------------------

This is not working, I am sorry, I m new in SQL

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 639 views
  • 0 likes
  • 2 in conversation