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?
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;
@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.
@souji wrote:
What is output look like?
See Maxim 4.
Hi @souji
Hereafter is what First. and Last. variables look like if you group your data by X and then by Y:
-> 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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.