BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cmemtsa
Quartz | Level 8
data T;
input ID A;
Datalines;
1310 CA
1310 AB
1310 CD 1320 AB 1320 CA ;

Hello,

how can I extract only the first encountered records per ID? i.e.

1310 CA

1320 AB

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

If your data is sorted then

 

data want;
   set have;
   by id;
   if first.id;
run;

When you use a By statement, which requires the data to be sorted by the variables on the statement and if you mix ascending and descending order you need to provide the same information as Proc Sort would use to sort the values, the data step creates automatic variables to identify the first and last record of each by group. You access the values using First. or Last. (dot immediately after the word first or last and before the variable name). These variables have numeric values of 1 (true) and 0 (false) and so can be used in If statements. The automatic variables are not written to the output data.

A simple: If <condition>; is known as a "subsetting if" and only records that have a true value for the condition are kept.

 

BTW your data step fails to run. You did not tell the input that the values of the variable A are character.

View solution in original post

2 REPLIES 2
ballardw
Super User

If your data is sorted then

 

data want;
   set have;
   by id;
   if first.id;
run;

When you use a By statement, which requires the data to be sorted by the variables on the statement and if you mix ascending and descending order you need to provide the same information as Proc Sort would use to sort the values, the data step creates automatic variables to identify the first and last record of each by group. You access the values using First. or Last. (dot immediately after the word first or last and before the variable name). These variables have numeric values of 1 (true) and 0 (false) and so can be used in If statements. The automatic variables are not written to the output data.

A simple: If <condition>; is known as a "subsetting if" and only records that have a true value for the condition are kept.

 

BTW your data step fails to run. You did not tell the input that the values of the variable A are character.

cmemtsa
Quartz | Level 8
Thank you!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 600 views
  • 1 like
  • 2 in conversation