DATA Step, Macro, Functions and more

problem with the program

Reply
Highlighted
Occasional Contributor
Posts: 13

problem with the program

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

Super User
Super User
Posts: 8,605

Re: problem with the program

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;
Occasional Contributor
Posts: 13

Re: problem with the program

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 

Super User
Posts: 21,449

Re: problem with the program

[ Edited ]

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. 

Super User
Super User
Posts: 8,605

Re: problem with the program

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

Occasional Contributor
Posts: 13

Re: problem with the program

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.

Super User
Super User
Posts: 8,605

Re: problem with the program

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

Ask a Question
Discussion stats
  • 6 replies
  • 168 views
  • 0 likes
  • 3 in conversation