BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Junyong
Pyrite | Level 9

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)

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

@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.

 

 

View solution in original post

7 REPLIES 7
Junyong
Pyrite | Level 9
The code is just a working example I made, and I used IT to make the panel data set unbalanced. There is no IT in the real data sets.
Tom
Super User Tom
Super User

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
Pyrite | Level 9
It seems I need to explicitly create the FIRST variable before any PROC (or any DATA). Thanks.
Reeza
Super User
You can't use LAG in WHERE because WHERE follows the SQL limitations and doesn't have access to the other rows. IF is a data set construct instead.
Junyong
Pyrite | Level 9
So I need to subset (or create the FIRST variable) it in DATA before PROCs or DATAs. Thank you.
Reeza
Super User

@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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 7 replies
  • 658 views
  • 2 likes
  • 3 in conversation