BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Midi
Obsidian | Level 7

Hello Everyone.

I have This DataSet:

   

data test;
input Feature_ID Client_ID;
cards;
52004 541111
56222 541111
56300 541111
73222 980002 
73600 980002
78006 980002
85000 980002
95001 1000001
98020 1000001
;
run;

 And I want to Create a Flag Column , That Takes 1 for the Maximum Feature_ID for Each Client .

The Result Should be as follow:

 

data test;
input Feature_ID Client_ID Flag;
cards;
52004 541111 0
56222 541111 0
56300 541111 1
73222 980002 0
73600 980002 0
78006 980002 0
85000 980002 1
95001 1000001 0
98020 1000001 1
;
run;

How Can I do it ?

 

What I've done (Because the Original Data was not ordered) , i started by ordering the Data , using Proc Sql , This Way:

proc sql;
   create table tab_Trial as select
               Feature_ID
              ,Client_ID
           from Test
           order by Feature_ID, Client_ID;
quit;

And Then Tried this Code , to Create the Flag Column :

 

data Flagging; 
set Tab_Trial; 
Flag=ifn(max(Feature_ID),1,0); 
by Client_ID;
run;

But it didn't worked , it marked every line with 1 , my guess is that , i have to use First.Feature_ID but alongside the Max() function is what got me confused , any help would be much appreciated.

 

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
KachiM
Rhodochrosite | Level 12

Try this:

data Flagging;
   do until(last.Client_ID);
      set Tab_Trial;
      by Client_ID;
      Flag = ifN(last.Client_ID, 1, 0);
      output;
   end;
run;

View solution in original post

4 REPLIES 4
KachiM
Rhodochrosite | Level 12

Try this:

data Flagging;
   do until(last.Client_ID);
      set Tab_Trial;
      by Client_ID;
      Flag = ifN(last.Client_ID, 1, 0);
      output;
   end;
run;
Midi
Obsidian | Level 7

Thank u , very much , i got where i messed up.

Thank you again.

novinosrin
Tourmaline | Level 20

Hello @Midi 

 



data test;
input Feature_ID Client_ID;
cards;
52004 541111
56222 541111
56300 541111
73222 980002 
73600 980002
78006 980002
85000 980002
95001 1000001
98020 1000001
;
run;
/*Using Proc SQL*/
proc sql;
create table want as
select *,max(feature_id)=feature_id as Flag
from test
group by client_id
order by client_id ,feature_id;
quit;

/*Data step if your dataset is sorted by client_id feature_id */

data want;
set test;
by client_id;
Flag=last.client_id=1;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 4 replies
  • 1428 views
  • 2 likes
  • 3 in conversation