Help using Base SAS procedures

sorting a dataset

Accepted Solution Solved
Reply
Super Contributor
Posts: 301
Accepted Solution

sorting a dataset

Hi everyone, I got the next dataset with three variables, sorting by subjid and dpfdate:

        txt

    1 SLOW INCRE
    2 ROUTINE DO
    3 ROUTINE DO
    4 ROUTINE DO
    5 ROUTINE DO
    6 ROUTINE DO
    7 ROUTINE DO
    8 ROUTINE DO
    9 ROUTINE DO
  10 ROUTINE DO
  11 ROUTINE DO
  12 ROUTINE DO
  13 ROUTINE DO
  14 ROUTINE DO
  15 ROUTINE DO
  16 ROUTINE DO
  17 ROUTINE DO
  18 ROUTINE DO
  19 ROUTINE DO

Obs  DPFDATE    SUBJID

    1 13DEC2006    10051001
    2 14DEC2006    10051001
    3 15DEC2006    10051001
    4 16DEC2006    10051001
    5 17DEC2006    10051001
    6 18DEC2006    10051001
    7 19DEC2006    10051001
    8 20DEC2006    10051001
    9 21DEC2006    10051001
  10 22DEC2006    10051001
  11 23DEC2006    10051001
  12 24DEC2006    10051001
  13 25DEC2006    10051001
  14 26DEC2006    10051001
  15 27DEC2006    10051001
  16 28DEC2006    10051001
  17 29DEC2006    10051001
  18 30DEC2006    10051001
  19 31DEC2006    10051001

I would like tocreate a dataset something like:

1 slow increase  13dec2006  10051001

2routine do          14dec2006  10051001

3routine do          31dec2006  10051001

I mean: sorting out by subjid and dpfdate but picking up only the first and the last record of every txt variable.

Thanks


Accepted Solutions
Solution
‎03-19-2012 11:17 AM
Respected Advisor
Posts: 3,156

sorting a dataset

I am not sure what you are really aiming for, but given the data as is, the following code can get what you want:

data have;

input txt &$11. DPFDATE  :date9.  SUBJID :$8.;

format dpfdate date9.;

cards;

SLOW INCRE   13DEC2006    10051001

ROUTINE DO   14DEC2006    10051001

ROUTINE DO   15DEC2006    10051001

ROUTINE DO   16DEC2006    10051001

ROUTINE DO   17DEC2006    10051001

ROUTINE DO   18DEC2006    10051001

ROUTINE DO   19DEC2006    10051001

ROUTINE DO   20DEC2006    10051001

ROUTINE DO   21DEC2006    10051001

ROUTINE DO  22DEC2006    10051001

ROUTINE DO  23DEC2006    10051001

ROUTINE DO  24DEC2006    10051001

ROUTINE DO  25DEC2006    10051001

ROUTINE DO  26DEC2006    10051001

ROUTINE DO  27DEC2006    10051001

ROUTINE DO  28DEC2006    10051001

ROUTINE DO  29DEC2006    10051001

ROUTINE DO  30DEC2006    10051001

ROUTINE DO  31DEC2006    10051001

;

data want;

set have;

by txt notsorted;

if (first.txt or last.txt);

run;

proc print;run;

Regards,

Haikuo

View solution in original post


All Replies
Solution
‎03-19-2012 11:17 AM
Respected Advisor
Posts: 3,156

sorting a dataset

I am not sure what you are really aiming for, but given the data as is, the following code can get what you want:

data have;

input txt &$11. DPFDATE  :date9.  SUBJID :$8.;

format dpfdate date9.;

cards;

SLOW INCRE   13DEC2006    10051001

ROUTINE DO   14DEC2006    10051001

ROUTINE DO   15DEC2006    10051001

ROUTINE DO   16DEC2006    10051001

ROUTINE DO   17DEC2006    10051001

ROUTINE DO   18DEC2006    10051001

ROUTINE DO   19DEC2006    10051001

ROUTINE DO   20DEC2006    10051001

ROUTINE DO   21DEC2006    10051001

ROUTINE DO  22DEC2006    10051001

ROUTINE DO  23DEC2006    10051001

ROUTINE DO  24DEC2006    10051001

ROUTINE DO  25DEC2006    10051001

ROUTINE DO  26DEC2006    10051001

ROUTINE DO  27DEC2006    10051001

ROUTINE DO  28DEC2006    10051001

ROUTINE DO  29DEC2006    10051001

ROUTINE DO  30DEC2006    10051001

ROUTINE DO  31DEC2006    10051001

;

data want;

set have;

by txt notsorted;

if (first.txt or last.txt);

run;

proc print;run;

Regards,

Haikuo

Super Contributor
Posts: 1,636

sorting a dataset

/* I add a observation to your original data: 

SLOW INCRE   23DEC2007    10051001 */

/* borrowed Haikuo's dataset */

data have;

input txt &$11. DPFDATE  :date9.  SUBJID :$8.;

format dpfdate date9.;

cards;

SLOW INCRE   13DEC2006    10051001

ROUTINE DO   14DEC2006    10051001

ROUTINE DO   15DEC2006    10051001

ROUTINE DO   16DEC2006    10051001

ROUTINE DO   17DEC2006    10051001

ROUTINE DO   18DEC2006    10051001

ROUTINE DO   19DEC2006    10051001

ROUTINE DO   20DEC2006    10051001

ROUTINE DO   21DEC2006    10051001

ROUTINE DO  22DEC2006    10051001

ROUTINE DO  23DEC2006    10051001

ROUTINE DO  24DEC2006    10051001

ROUTINE DO  25DEC2006    10051001

ROUTINE DO  26DEC2006    10051001

ROUTINE DO  27DEC2006    10051001

ROUTINE DO  28DEC2006    10051001

ROUTINE DO  29DEC2006    10051001

ROUTINE DO  30DEC2006    10051001

ROUTINE DO  31DEC2006    10051001

SLOW INCRE   23DEC2007    10051001

;

data temp;

set have;

n=_n_;

proc sort;

by txt n;

data want;

  set temp;

by txt;

if (first.txt or last.txt);

run;

proc print;run;

Obs       txt          DPFDATE     SUBJID      n

1     ROUTINE DO    14DEC2006    10051001     2

2     ROUTINE DO    31DEC2006    10051001    19

3     SLOW INCRE    13DEC2006    10051001     1

4     SLOW INCRE    23DEC2007    10051001    20

Linlin

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 155 views
  • 3 likes
  • 3 in conversation