BookmarkSubscribeRSS Feed
souji
Obsidian | Level 7

 

Can you please make me understand , how the second by variable read it out put : if first.y ;

 

X

Y

Z

1

A

27

1

A

33

1

B

45

2

A

52

2

B

69

3

B

70

4

A

82

4

C

91

The following SAS program is submitted:

data two;
   set one;
   by x y;
   if first.y;
run;

proc print data=two noobs;
run;

 

What is output look like?

 

4 REPLIES 4
unison
Lapis Lazuli | Level 10

 

Here's the full code.

 

data one;
	input x y $ z;
	datalines;
1 A 27
1 A 33
1 B 45
2 A 52
2 B 69
3 B 70
4 A 82
4 C 91
;
run;

proc sort data=one;
	by x y;
run;

data two;
	set one;
	by x y;

	if first.y;
run;

proc print data=two;
run;

 

You're going to get only the first observation of each combination of x,y. In this example, the 2nd observation will not be output (since x=1, y=A appears in the 1st observation). 

 

To better understand what is happening with first.y, I'd recommend submitting the following and inspecting fy:

data two;
	set one;
	by x y;

	fy = first.y;
/* 	if first.y; */
run;

proc print data=two;
run;

 

 

-unison
SASKiwi
PROC Star

@souji  - This question is explained in the documentation: https://documentation.sas.com/?docsetId=lrcon&docsetTarget=n01a08zkzy5igbn173zjz82zsi1s.htm&docsetVe...

 

You will learn more quickly if you check the documentation first, and then if the answer is still not clear, post in the Communities.

 

Also questions like "What is output look like?" you can answer yourself by running the program.

ed_sas_member
Meteorite | Level 14

Hi @souji 

Hereafter is what First. and Last. variables look like if you group your data by X and then by Y:

 

Capture d’écran 2020-01-05 à 10.20.26.png

 

-> for X, the first variable of the BY statement is quite intuitive -> for each new value of X, in the order shown, the first.X variable take the value 1. Otherwise, it is set to 0.

-> for the Y variable, for each new group of X and each new value of Y inside those groups, the first.Y variable is set to 1. Otherwise, it is set to 0.

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
  • 4 replies
  • 554 views
  • 1 like
  • 5 in conversation