BookmarkSubscribeRSS Feed
robertrao
Quartz | Level 8

Hi Team,

I have data like shown.

the data is sorted already and if there first change of the codes variable i want to stop there and dont want records after that...

After acheiving that i want the first one of the first codes and the last one as shown

HAVE:

ID        TIME                                         CODES

101     02JAN2013 :18:30                       EMNG

101     02JAN2013:18:47                         EMNG

101     02JAN2013 :18:47                       EMNG

101     02JAN2013:23:08                      OMNG

101     02JAN2013:23:08                      LMNG

101     02JAN2013:23:59                     DMNG

101     02JAN2013:23:59                     PLNH

WANT:

101     02JAN2013 :18:30                       EMNG

101     02JAN2013:23:08                      OMNG

Thanks

10 REPLIES 10
ballardw
Super User

Is there a reason you Want example doesn't include the LMNG, DMNG or PLNH codes?

robertrao
Quartz | Level 8

I just wanted UNTIL the first change

Thanks

Reeza
Super User

Step 1 - enumerate your groups, count the groups and then the records within those.

Step 2 - determine criteria that meet your conditions

Step 3 - include those on a where clause to get your output.

There are fancier ways, but for a beginner level this is the easiest method to understand.

SAS FAQ: How can I create an enumeration variable by groups?

Kavitha
Calcite | Level 5

Hi,  can you please explain your requirement in a more detailed way

why your want data set does not have

101 02JAN2013:23:08                  LMNG

??

robertrao
Quartz | Level 8

Hi Kavitha,

Initial IS EMNG and the first change(in code) from there is OMNG.after that i dont want any information.

i need to know what the first change is?

Thanks

Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

so you only need  the 1st records of the 1st two by groups. I would first take the 1st record of each by group(codes) and then narrow the result to the 1st two records.

But i am sure the experts could come up with some fancy ways of doing it Smiley Happy

Kavitha
Calcite | Level 5

Hi Pls try the below code....

data have;

   input ID:$3.   TIME & datetime.  CODES :$4.;

   format time datetime.;

   cards;

101     02JAN2013 :18:30                       EMNG

101     02JAN2013:18:47                         EMNG

101     02JAN2013 :18:47                       EMNG

101     02JAN2013:23:08                      OMNG

101     02JAN2013:23:08                      LMNG

101     02JAN2013:23:59                     DMNG

101     02JAN2013:23:59                     PLNH

102     02JAN2013 :18:30                       AAAA

102     02JAN2013:18:47                         AAAA

102     02JAN2013 :18:47                       AAAA

102     02JAN2013:23:08                      AAAA

102     02JAN2013:23:59                      BBBB

103     02JAN2013 :18:30                       DDDD

104     02JAN2013 :18:30                       EEEE

104     02JAN2013:18:47                         EEEE

;;;;

   run;

data want(drop=temp_count temp_code);

    set have;

    BY ID;

    retain temp_count temp_code;

    if(first.id) then do

        temp_code=CODES;

        temp_count=0;

        output want;

    end;

    if(temp_count eq 0) then do;

        if(codes ne temp_code) then do

            temp_count=1;

            output want;

        end;

        else   

            delete;

    end;

run;

barchan
Calcite | Level 5

The following code extracts the very first record and the first record after change of CODES:

data want(drop=xcode flag);

length xcode $4;

do until(last.id);

    set have;

    by id;

    if first.id then do;

       output;

       xcode=codes;

    end;

    if codes^=xcode then do;

       if ^flag then output;

       flag=1;

    end;

end;

run;

data_null__
Jade | Level 19
data codes;
   input ID:$3.   TIME & datetime.  CODES :$4.;
  
format time datetime.;
  
cards;
101     02JAN2013 :18:30                       EMNG
101     02JAN2013:18:47                         EMNG
101     02JAN2013 :18:47                       EMNG
101     02JAN2013:23:08                      OMNG
101     02JAN2013:23:08                      LMNG
101     02JAN2013:23:59                     DMNG
101     02JAN2013:23:59                     PLNH
;;;;
   run;
proc print;
  
run;
data first2;
   set codes;
   by id codes notsorted;
  
if first.id then c=0;
  
if first.codes then c+1;
  
if c le 2 and first.codes then output;
  
drop c;
   run;
proc print;
  
run;
Amir
PROC Star

Hi,

Another way, amongst many:

data want(drop=first_code changed_code);

  set have;

  output;

  first_code=codes;

  do until(changed_code);

    set have;

    changed_code=(first_code ne codes);

    if changed_code then

    do;

      output;

      stop;

    end;

  end;

run;

Regards,

Amir.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 10 replies
  • 1415 views
  • 1 like
  • 8 in conversation