DATA Step, Macro, Functions and more

SAS program to read the nth observation from a dataset by direct access

Reply
Contributor
Posts: 42

SAS program to read the nth observation from a dataset by direct access

Dear All,

Is it possible to write a SAS Program to read the nth observation from  a dataset by Direct Access ?

Thanks a lot in advance for help.

Regards,

S Ravuri.

Trusted Advisor
Posts: 1,300

SAS program to read the nth observation from a dataset by direct access

every 3rd record.

data foo;

do _n_=1 to 19 by 3;

set sashelp.class point=_n_;

output;

end;

stop;

run;

NOTE: The data set WORK.FOO has 7 observations and 5 variables.

NOTE: DATA statement used (Total process time):

      real time           0.00 seconds

      cpu time            0.00 seconds

only the 5th record.

data foo;

_n_=5;

set sashelp.class point=_n_;

output;

stop;

run;

NOTE: The data set WORK.FOO has 1 observations and 5 variables.

NOTE: DATA statement used (Total process time):

      real time           0.00 seconds

      cpu time            0.00 seconds

N/A
Posts: 1

Re: SAS program to read the nth observation from a dataset by direct access

Please give me the answer:

1: SAS program to read alternative observations from a datasset by direct access?

2: SAS program to read 5th 8th and 10th obesrvations from a datasset by direct access?

Respected Advisor
Posts: 4,654

Re: SAS program to read the nth observation from a dataset by direct access

Simply adapt 's code :

/* Alternate observations */

data foo1;

do _n_ = 1 to nobs by 2;

     set sashelp.class point=_n_ nobs=nobs;

     output;

     end;

stop;

run;

 

/* Observations 5, 8 and 10 */

data foo2;

do _n_ = 5, 8, 10;

     set sashelp.class point=_n_;

     output;

     end;

stop;

run;

PG

PG
Trusted Advisor
Posts: 1,300

Re: SAS program to read the nth observation from a dataset by direct access

There may be a simpler way, but this is how I like to approach it.

data foo;

    *get obs 5,8,10 of 10 obs cycle;

    *n[0] tracks position through cycle;

    *n[1]-n[3] hold interation movement;

    *_n_ start at 5, then move by n[1]=3 to 8,

    *                then move by n[2] to 10,

    *                then move by n[3] to 15,

    *reset n[0]=1, repeat;

    array n[0:3] _temporary_ ( 1 3 2 5 );

    do _n_=5 by n[n[0]] while( _n_ <= nobs );

        set sashelp.shoes nobs=nobs point=_n_;

        n[0] = ifn( n[0]=3 , 1 , n[0]+1 );

     output;

      end;

  stop;

  run;

N/A
Posts: 1

Re: SAS program to read the nth observation from a dataset by direct access

/* for nth observation of different group in same data set*/
data ds1 drop(var1);

set ds2;

By var_name;

retain var1;

if first.var_name=1 and last.var_name=0 then var1=0;

var1+1;

if var1=nth_observation then output;

run;

Ask a Question
Discussion stats
  • 5 replies
  • 10267 views
  • 1 like
  • 5 in conversation