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

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
Barite | Level 11

Thank you for your help.

I got it.

Have a nice weekend.

HHC

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 664 views
  • 6 likes
  • 4 in conversation