Suppose an unbalanced panel data set as follows.
data RAW(drop=IT);
call streaminit(1);
do I=1 to 500;
IT=rand("poisson",5);
do T=IT to IT+rand("poisson",5);
X=rand("normal");
output;
end;
end;
run;
There are 500 Is with random but continuous Ts, then one may subset it as follows.
data SUBSET;
set RAW;
by I T;
if first.I;
run;
Or indirectly without FIRST,
data SUBSET;
set RAW;
by I T;
if I^=lag1(I);
run;
Both use IF rather than WHERE to subset. Can I do this with WHERE rather than IF? I tried the following but unsuccessful. Thanks.
data SUBSET;
set RAW(where=(first.I=1));
run;
(failed as FIRST works only after SET and BY)
data SUBSET;
set RAW(where=(I^=lag1(I)));
run;
(failed as LAGs are invalid in WHERE)
proc means data=SUBSET(where=(I^=lag1(I)));
var X;
run;
(failed similarly)
@Junyong wrote:
So I need to subset (or create the FIRST variable) it in DATA before PROCs or DATAs. Thank you.
Yes, you may want to just add a flag via a view though as suggested via @Tom
data viewTEmp / view=viewTemp;
set have;
by myGroups;
if first.group then first_flag=1;else first_flag=0;
run;
Then in future queries, just hit the view instead of the main table and you can access the first_flag variable.
proc means data=viewTemp;
where first_flag=1;
run;
Unfortunately this does require that your original data is pre sorted. You could probably write a more complicated SQL view that did this as well, if you need to sort first.
Did you try:
where T=IT;
Then the answer is no. Use the whatever IF statement that works.
Make a view if it bothers you that much.
data first / view=first;
set have;
by id;
if first.id;
run;
@Junyong wrote:
So I need to subset (or create the FIRST variable) it in DATA before PROCs or DATAs. Thank you.
Yes, you may want to just add a flag via a view though as suggested via @Tom
data viewTEmp / view=viewTemp;
set have;
by myGroups;
if first.group then first_flag=1;else first_flag=0;
run;
Then in future queries, just hit the view instead of the main table and you can access the first_flag variable.
proc means data=viewTemp;
where first_flag=1;
run;
Unfortunately this does require that your original data is pre sorted. You could probably write a more complicated SQL view that did this as well, if you need to sort first.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.