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
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;
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;
Thank u , very much , i got where i messed up.
Thank you again.
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;
Thank you a lot.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.