BookmarkSubscribeRSS Feed
Dynamike
Calcite | Level 5

Hi all,

 

i have the following:

 

I want to define a minimum regarding ID as main variable (which works).

 

ID  MIN_DATE

1   01/02/2015

 

Proc Sort Data=have;
by ID YEAR MONTH;
run;

 

Data MIN;
set have;
by ID;
MIN_YEAR=YEAR;
MIN_MONTH=MONTH;
MIN_DATE=MDY(MIN_MONAT,1,MIN_JAHR);
format MIN_DATE ddmmyy10.;
If First.ID then output;
Run;

 

Now i want to define a minimum for ID as well as for another subvariable (X) and this should look like this:

 

ID   X  MIN_DATE

1   a  01/02/2015

1   b  01/04/2016

 

i tried this:

 

Proc Sort Data=have;
by ID X YEAR MONTH;
run;

 

Data MIN;
set have;
by ID X;
MIN_YEAR=YEAR;
MIN_MONTH=MONTH;
MIN_DATE=MDY(MIN_MONAT,1,MIN_JAHR);
format MIN_DATE ddmmyy10.;
If First.ID AND FIRST.X then output;
Run;

 

But my subvariable X is not considered.....its just the same as above.

 

Could you help me?

 

Best regards

Mike

5 REPLIES 5
Dynamike
Calcite | Level 5

Sorry my fault: VERSID is the same as ID.

I have corrected it in the original post.

 

Thank you for your advice!

Kurt_Bremser
Super User

Your problem is the use of

first.id and first.x

as that will only be true for the first occurence of an id.

A reduced subsetting if will work:

data have;
input id x $ year month;
cards;
1 a 2018 1
1 a 2018 3
1 a 2018 2
1 b 2017 12
1 b 2017 3
;
run;

Proc Sort Data=have;
by ID X YEAR MONTH;
run;

Data MIN;
set have;
by ID X;
MIN_YEAR=YEAR;
MIN_MONTH=MONTH;
MIN_DATE=MDY(MIN_MONAT,1,MIN_JAHR);
format MIN_DATE ddmmyy10.;
If FIRST.X;
Run;
FreelanceReinh
Jade | Level 19

 

... and make sure that you use consistent variable names (see MIN_YEAR vs. MIN_JAHR). To improve efficiency, you could move the three assignment statements MIN_...=... together with the OUTPUT statement into a DO-END block:

if first.x then do; ...; end;

 or, even simpler, put "if first.x;" directly after the BY statement.

Tom
Super User Tom
Super User

If you want to get one observation per ID,X group then just reference FIRST.X.  If you include FIRST.ID you are looking for only one observation per ID group.

 

If you do include FIRST.ID there is no need to also include FIRST.X.  When you are on the first observation for the current ID group you are by definition on the first observation for the current X group.  

 

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
  • 5 replies
  • 754 views
  • 0 likes
  • 4 in conversation