Hi Team,
I have data like shown.
the data is sorted already and if there first change of the codes variable i want to stop there and dont want records after that...
After acheiving that i want the first one of the first codes and the last one as shown
HAVE:
ID TIME CODES
101 02JAN2013 :18:30 EMNG
101 02JAN2013:18:47 EMNG
101 02JAN2013 :18:47 EMNG
101 02JAN2013:23:08 OMNG
101 02JAN2013:23:08 LMNG
101 02JAN2013:23:59 DMNG
101 02JAN2013:23:59 PLNH
WANT:
101 02JAN2013 :18:30 EMNG
101 02JAN2013:23:08 OMNG
Thanks
Is there a reason you Want example doesn't include the LMNG, DMNG or PLNH codes?
I just wanted UNTIL the first change
Thanks
Step 1 - enumerate your groups, count the groups and then the records within those.
Step 2 - determine criteria that meet your conditions
Step 3 - include those on a where clause to get your output.
There are fancier ways, but for a beginner level this is the easiest method to understand.
SAS FAQ: How can I create an enumeration variable by groups?
Hi, can you please explain your requirement in a more detailed way
why your want data set does not have
101 | 02JAN2013:23:08 | LMNG |
??
Hi Kavitha,
Initial IS EMNG and the first change(in code) from there is OMNG.after that i dont want any information.
i need to know what the first change is?
Thanks
so you only need the 1st records of the 1st two by groups. I would first take the 1st record of each by group(codes) and then narrow the result to the 1st two records.
But i am sure the experts could come up with some fancy ways of doing it
Hi Pls try the below code....
data have;
input ID:$3. TIME & datetime. CODES :$4.;
format time datetime.;
cards;
101 02JAN2013 :18:30 EMNG
101 02JAN2013:18:47 EMNG
101 02JAN2013 :18:47 EMNG
101 02JAN2013:23:08 OMNG
101 02JAN2013:23:08 LMNG
101 02JAN2013:23:59 DMNG
101 02JAN2013:23:59 PLNH
102 02JAN2013 :18:30 AAAA
102 02JAN2013:18:47 AAAA
102 02JAN2013 :18:47 AAAA
102 02JAN2013:23:08 AAAA
102 02JAN2013:23:59 BBBB
103 02JAN2013 :18:30 DDDD
104 02JAN2013 :18:30 EEEE
104 02JAN2013:18:47 EEEE
;;;;
run;
data want(drop=temp_count temp_code);
set have;
BY ID;
retain temp_count temp_code;
if(first.id) then do
temp_code=CODES;
temp_count=0;
output want;
end;
if(temp_count eq 0) then do;
if(codes ne temp_code) then do
temp_count=1;
output want;
end;
else
delete;
end;
run;
The following code extracts the very first record and the first record after change of CODES:
data want(drop=xcode flag);
length xcode $4;
do until(last.id);
set have;
by id;
if first.id then do;
output;
xcode=codes;
end;
if codes^=xcode then do;
if ^flag then output;
flag=1;
end;
end;
run;
Hi,
Another way, amongst many:
data want(drop=first_code changed_code);
set have;
output;
first_code=codes;
do until(changed_code);
set have;
changed_code=(first_code ne codes);
if changed_code then
do;
output;
stop;
end;
end;
run;
Regards,
Amir.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.