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