BookmarkSubscribeRSS Feed
sambasiva_ravuri_tcs_com
Calcite | Level 5

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.

5 REPLIES 5
FriedEgg
SAS Employee

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

Jagadeesh2196
Calcite | Level 5

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?

PGStats
Opal | Level 21

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
FriedEgg
SAS Employee

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;

MohammadAmirNaqvi
Calcite | Level 5

/* 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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 28787 views
  • 1 like
  • 5 in conversation