BookmarkSubscribeRSS Feed
makset7
Obsidian | Level 7

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

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
makset7
Obsidian | Level 7
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 

Reeza
Super User

@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. 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Please re-read the code I provided you with, you will come across a "proc sort" which is used to sort the data.

makset7
Obsidian | Level 7

my solution Smiley Happy

 

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Why make it more complicated, my code should do it far simpler.

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 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.

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