Help using Base SAS procedures

SUBSETTING

Reply
Super Contributor
Posts: 1,040

SUBSETTING

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

Super User
Posts: 10,516

Re: SUBSETTING

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

Super Contributor
Posts: 1,040

Re: SUBSETTING

I just wanted UNTIL the first change

Thanks

Super User
Posts: 17,868

Re: SUBSETTING

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?

Occasional Contributor
Posts: 9

Re: SUBSETTING

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

??

Super Contributor
Posts: 1,040

Re: SUBSETTING

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

Super Contributor
Super Contributor
Posts: 440

Re: SUBSETTING

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

Occasional Contributor
Posts: 9

Re: SUBSETTING

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;

Occasional Contributor
Posts: 7

Re: SUBSETTING

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;

Respected Advisor
Posts: 3,777

Re: SUBSETTING

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;
Super Contributor
Posts: 282

Re: SUBSETTING

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.

Ask a Question
Discussion stats
  • 10 replies
  • 452 views
  • 1 like
  • 8 in conversation