BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

I don't understand how first variable's values are determined when there are two first variables.

For example:

When I use two first statements then I don't understand the result o the second first statement.

Related to  first.group2 I was sure that it looks only on values of group2 and give value 1 to first value.

Values in group2 are:              one,one,one,one.one.one.one,two,two,two,two

so I was sure that I will get: 1,0,0,0,0,0,0,1,0,0,0

But I see that I got values:  1,0,0,0,1,0,0,1,0,0,0

 

Can anyone explain please?

Thank you so much

 

 

    

 

 

Data raw_tbl;
input ID group1 $  group2 $ Y;
cards;
1 a One 5
2 a One 10
3 a One 15
4 a One 20
5 b One 25
6 b One 30
7 b One 35
8 c Two 40
9 c Two 45
10 c Two .
11 c Two 50
;
run;

 data zz;
set raw_tbl;
by group1 group2;
first_group1=first.group1;
first_group2=first.group2;
Run;
 

data ww;
set raw_tbl;
by group2;
first_group2=first.group2;
Run;

 

 

  

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

This is because in zz, you have two variables in your By Statement. Group1 is considered first. Group2 is considered within Group1. This means that Group2 in obs5 is the first value in the Group1 group of 'b'.

Kurt_Bremser
Super User

When you use more than one variable in a by statement, you build a hierarchy from left to right. A change in a variable further up (left) forces a change in all "lower" variables (to the right).

by a b c;

As soon as first.a is 1, first.b and first.c will also be set 1. If first.a is 0, but first.b is 1, then first.c will also be 1, even if lag(c) = c.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 400 views
  • 1 like
  • 3 in conversation