Hello
I have the following sample data set:
data exe;
input n $ x $ y;
datalines;
1 7 .
2 4 .
3 5 .
4 6 .
5 3 .
6 2 .
7 1 .
1 4 .
2 1 .
3 3 .
4 5 .
5 7 .
6 2 .
7 6 .
1 3 .
2 7 .
/****/
;
run;
I would like to determine the order of the maximum (or minimum) values that are to be achieved
the maximum value in the data set is 7 and is in the first place (n = 1), the next maximum value in the set is 6 (n = 4) (skipping the first max values), the next maximum value in the set is 5 (n = 3) (omitting the previous maximum values) and so on.
I would like to get the result as in the annex
thank you in advance for your answer
Best regard
Please use the post area to show required output, not attachments. from your data, you can simply sort, apply your logic, then sort back again:
proc sort data=exe; by x n; run; data exe; set exe; by n; if first.n then y=123; run; proc sort data=exe; by n x; run;
data exe; set exe; by n; if first.n then y=123; run;
ERROR: variables BY are not sorted correctly in the WORK.EXE file
@makset7 wrote:
data exe; set exe; by n; if first.n then y=123; run;ERROR: variables BY are not sorted correctly in the WORK.EXE file
Did you sort your data set first using PROC SORT? In general you shouldn't use the same data set name in DATA and SET, it's a good way to destroy your data, which you may have done already so make sure to recreate it from the import step first or whatever step creates the exe data.
Please re-read the code I provided you with, you will come across a "proc sort" which is used to sort the data.
my solution
data exe;
input n $ x;
datalines;
1 7
2 4
3 5
4 6
5 3
6 2
7 1
1 4
2 1
3 3
4 5
5 7
6 2
7 6
1 3
2 7
;
run;
data exe;
set exe;
if mod(_n_ , 7) = 0 then n1 = int(_n_ / 7);
if mod(_n_ , 7) ne 0 then n1 = int(_n_ / 7) + 1;
run;
proc sort data = exe;
key n1 / ascending;
key x / descending;
run;
data exe;
set exe;
z = (lag6(n) * 1000000) + (lag5(n) * 100000) + (lag4(n) * 10000) + (lag3(n) * 1000) + (lag2(n) * 100) + (lag1(n) * 10) + n;
if mod(_n_ , 7) ne 0 then z = .;
run;
maybe you have easier solutions ??? if so, write
RW9 thank you for your help.
Why make it more complicated, my code should do it far simpler.
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!
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.