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