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

Hi,

I have the following data:

data have;
input number sequence @@;
datalines;
1 1 2 1 3 1 4 2 5 1 6 2
7 1 8 1 9 3 10 1
;
run;

What I would like to obtain are the observations for which the sequence =1 and the sequence of the number right after it is greater than 1:

number sequence
3 1
4 2
5 1
6 2
8 1
9 3

 

I was trying to solve this problem with a temporary array that serves as a rolling window:

data want;
set have;
array seq [2] _temporary_;

seq [*] = sequence;
if seq[2] > 1 and seq[1] = 1;
run;

But was getting an error for the part "seq[*] = sequence" that "Expecting an arihmetic expression"

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

One way to solve it is by using the lag function. e.g.:

 

data want (keep=out: rename=(out_sequence=sequence out_number=number));
  set have;
  last_sequence=lag(sequence);
  last_number=lag(number);
  if last_sequence eq 1 and sequence gt 1 then do;
    out_sequence=last_sequence;
    out_number=last_number;
    output;
    out_sequence=sequence;
    out_number=number;
    output;
  end;
run;

Art, CEO, AnalystFinder.com

View solution in original post

3 REPLIES 3
art297
Opal | Level 21

One way to solve it is by using the lag function. e.g.:

 

data want (keep=out: rename=(out_sequence=sequence out_number=number));
  set have;
  last_sequence=lag(sequence);
  last_number=lag(number);
  if last_sequence eq 1 and sequence gt 1 then do;
    out_sequence=last_sequence;
    out_number=last_number;
    output;
    out_sequence=sequence;
    out_number=number;
    output;
  end;
run;

Art, CEO, AnalystFinder.com

art297
Opal | Level 21

And here is another way to obtain the same result .. although I think using my first suggestion would be slightly faster:

 

data want (drop=_:);
  merge have have(firstobs=2 keep=sequence rename=(sequence=_sequence));
  retain _next;
  if sequence eq 1 and _sequence gt 1 then do;
    output;
    _next=1;
  end;
  else if _next then do;
    output;
    _next=0;
  end;
run;

Art, CEO, AnalystFinder.com

novinosrin
Tourmaline | Level 20

 

Hi, 

data want(drop=flag);
do  until (last.sequence);
set have end=last;
by sequence notsorted;
retain flag;
if last.sequence and sequence=1 and not last then do;
output;
flag=1;
end;
else if first.sequence and flag then do;
output;
flag=0;
end;
end;
run;

 

Regards,

Naveen Srinivasan

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 3 replies
  • 1178 views
  • 2 likes
  • 3 in conversation