BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hhchenfx
Rhodochrosite | Level 12

Hello Everyone,

I want to group observation anytime variable "time" in my data reach value 1 and group=group +1

So basically, the Want file should be the original added 1 more variable name group as below.

1 2 1

2 3 1

3 2 1

1 6 2

2 9 2

3 6 2

4 9 2

1 9 2

2 1 3

3 1 3

I guess the retain should be used but I am not sure how to get the result I want.

Any help is very much appreciated.

Have a nice weekend.

HHC

data want;

input time value;

datalines;

1 2

2 3

3 2

1 6

2 9

3 6

4 9

1 9

2 1

3 1

;run;

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

The code of sir Arthur is good. Here is just an alternative.

data have;

  input time value;

  order=_n_;

  datalines;

1 2

2 3

3 2

1 6

2 9

3 6

4 9

1 9

2 1

3 1

;

run;

proc sort data=have;

    by  time ;

run;

data want;

    set have;

    retain count;

    by time;

    if first.time then count=1;

    else count+1;

run;

proc sort data=want;

    by order;

run;

Thanks,

jagadish


Thanks,
Jag

View solution in original post

4 REPLIES 4
art297
Opal | Level 21

You weren't very clear about what you are trying to accomplish, thus I'll just guess:

data have;

  input time value;

  datalines;

1 2

2 3

3 2

1 6

2 9

3 6

4 9

1 9

2 1

3 1

;

run;

data want;

  set have;

  if _n_ eq 1 then group=1;

  if time lt lag(time) then group+1;

run;

RichardinOz
Quartz | Level 8

hhchenfx

Where Art uses group + 1 that implies the statement (automatically)

retain group ;


Richard

Jagadishkatam
Amethyst | Level 16

The code of sir Arthur is good. Here is just an alternative.

data have;

  input time value;

  order=_n_;

  datalines;

1 2

2 3

3 2

1 6

2 9

3 6

4 9

1 9

2 1

3 1

;

run;

proc sort data=have;

    by  time ;

run;

data want;

    set have;

    retain count;

    by time;

    if first.time then count=1;

    else count+1;

run;

proc sort data=want;

    by order;

run;

Thanks,

jagadish


Thanks,
Jag
hhchenfx
Rhodochrosite | Level 12

Thank you for your help.

I got it.

Have a nice weekend.

HHC

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 944 views
  • 6 likes
  • 4 in conversation