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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

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
  • 5 replies
  • 27283 views
  • 1 like
  • 5 in conversation