BookmarkSubscribeRSS Feed
monali
Obsidian | Level 7

I am trying to flag ID as 1 if they are present in different months. The following code seems to be flagging some records correctly and others incorrectly. I am not able to understand why some of them are not getting flagged (see the rows in RED). Please advise. 

 

data test1;
 set test;
 by id month;

    if first.month then do;
     flag = "Y";
   end;
run;

 

ID month flag
1 6 Y
1 7 Y
2 5 Y
2 6 Y
3 7  
3 7 Y
4 6 Y
4 7 Y
5 6 Y
5 7 Y
6 6 Y
7 7  
8 7  
8 7  
8 7  
8 7 Y
9 6  
9 7 Y
10 6  
10 7  
10 7 Y
11 6 Y
11 7 Y
12 6  
12 7 Y
13 6 Y
13 6  
13 7  
14 6 Y
14 7 Y
15 6 Y
15 7 Y
16 6 Y
16 7 Y
17 5 Y
17 6  
17 7 Y
3 REPLIES 3
PaigeMiller
Diamond | Level 26

The output you show for FLAG certainly does not correspond to the code you are showing. So either this isn't the code you used, or it isn't the data you used, or it isn't the actual output from the code shown.

 

Can you provide the input data set named TEST as a SAS data step, rather than any other method? Instructions

Can you provide the ENTIRE log for this code, with nothing chopped out, everything, all of it, every single line?


Also, your title doesn't seem to match the problem, could you go back to your original post and modify the title? Thanks

--
Paige Miller
ballardw
Super User

@monali wrote:

I am trying to flag ID as 1 if they are present in different months. The following code seems to be flagging some records correctly and others incorrectly. I am not able to understand why some of them are not getting flagged (see the rows in RED). Please advise. 

 

data test1;
 set test;
 by id month;

    if first.month then do;
     flag = "Y";
   end;
run;

 

ID month flag
1 6 Y
1 7 Y
2 5 Y
2 6 Y
3 7  
3 7 Y
4 6 Y
4 7 Y
5 6 Y
5 7 Y
6 6 Y
7 7  
8 7  
8 7  
8 7  
8 7 Y
9 6  
9 7 Y
10 6  
10 7  
10 7 Y
11 6 Y
11 7 Y
12 6  
12 7 Y
13 6 Y
13 6  
13 7  
14 6 Y
14 7 Y
15 6 Y
15 7 Y
16 6 Y
16 7 Y
17 5 Y
17 6  
17 7 Y

When I create data set that seems reasonable for your Test set and run your code:

data test;
   input id month;
datalines;
1 6
1 7
2 5
2 6
3 7
3 7
4 6
4 7
5 6
5 7
6 6
7 7
8 7
;

data test1;
 set test;
 by id month;

    if first.month then do;
     flag = "Y";
   end;
run;

The output is (with differences highlighted in red). Which is why I agree with @PaigeMiller that what you show is likely incomplete somewhere. Possibly a condition involving the ID variable? Or did you sort it before showing the results (which would be why the values for ID=3 and 8 are in different order)?

id month flag
1 6 Y
1 7 Y
2 5 Y
2 6 Y
3 7 Y
3 7  
4 6 Y
4 7 Y
5 6 Y
5 7 Y
6 6 Y
7 7 Y
8 7 Y
monali
Obsidian | Level 7

Yep, you are right! I was pulling data from a different table. This codes works! Thanks 

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!

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