BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DmytroYermak
Lapis Lazuli | Level 10

Hi all,

 

For instance a dataset is sorted by variable X.

 

How in one iteration reveal if an observation is only one in a group:

 

if first.X and last.X ...

 

Thank you! 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Thats it you have it.  

data want;
  set have;
  by x;
  if first.x and last.x then only_one="Yes";
  else only_one="No";
run;

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Thats it you have it.  

data want;
  set have;
  by x;
  if first.x and last.x then only_one="Yes";
  else only_one="No";
run;
DmytroYermak
Lapis Lazuli | Level 10
You are definitely right ). It was rather a "tough" day today (
RW9
Diamond | Level 26 RW9
Diamond | Level 26

No probs, it likely not be the first or last tough day!

srinath3111
Quartz | Level 8

Hi,

 

If you sort a data set  and in a data step when you specify by variable then sas creates two automatic variables back end they are first.by variable and last.by variable.

 

first.by variable value is=1 for the first observation in a dataset and rest of all the by group will be =0 except last.by variable. that will be one

 

You can observe it by writing example

 

proc sort data=ds;
by sex;
run;
data ds;
set sashelp.classfit;
by sex;
put _all_;'

if first.sex and last.x then flag=1;

else flag=0;
run;

 

Thanks

sas-innovate-2024.png

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.

 

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