SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
ZRick
Obsidian | Level 7


WANT TO assign _n_ number in ascending order based on sec, dt, rate in the last column.


data t;

input sec dt rate;

cards;

1 9205 0.2

1 9205 0.3

1 9205 0.4

1 9204 0.2

1 9204 0.3

1 9204 0.4

2 9205 0.1

2 9205 0.2

2 9204 0.1

2 9204 0.2

3 9205 0.8

3 9205 0.9

3 9205 1.1

3 9205 3.5

3 9205 4.2

;

/*

WANT TO assign _n_ number in ascending order based on rate in the last column

1 9205 0.2 1

1 9205 0.3 2

1 9205 0.4 3

1 9204 0.2 1

1 9204 0.3 2

1 9204 0.4 3

2 9205 0.1 1

2 9205 0.2 2

2 9204 0.1 1

2 9204 0.2 2

3 9205 0.8 1

3 9205 0.9 2

3 9205 1.1 3

3 9205 3.5 4

3 9205 4.2 5

*/

4 REPLIES 4
Reeza
Super User

data want;

set t;

retain count;

prev_value=lag(rate);

if value<prev_value then count=1;

else count+1;

run;

ZRick
Obsidian | Level 7

proc sort data=t;

by sec dt;

run;

data t;

set t;

by sec dt;

if first.rate then  n=1 ;

else n+1;

if last.rate then stop;

run;


It doesn't work, I don't know how to reset the n using first. and last. form to assign the n in ascending order.

Reeza
Super User

Change your first.rate to first.dt

And you don't need the last. and stop line.

joehinson
Calcite | Level 5

I agree with Reeza.

And if you need the kind of output you specified (group processing only, no sorting),

then you may want to add: NOTSORTED and skip pre-sorting altogether.

data t;

set t;

by sec dt NOTSORTED;

if first.dt then n=1;

else n+1;

run;

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1836 views
  • 0 likes
  • 3 in conversation