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

Hi Team,

 

I am stuck in the SAS QUery to filter the data.

The below example is for one account but my orginal data got 1million accounts.

Base data is the account level from 31jan2010 to 30APR2019

Sample data for one account.

COLL_IDNN_HKMAT_CLREFEND_OF_MONTHACT_CODE
2CD3B10131Jan2010 
2CD3B10128Feb2010 
2CD3B10131Mar2010 
2CD3B10130Apr2010 
2CD3B10131May2010 
2CD3B10130Jun2010 
2CD3B10131Jul2010 
2CD3B10131Aug2010 
2CD3B10130Sep2010 
2CD3B10131Oct2010 
2CD3B10130Nov2010 
2CD3B10131Dec2010 
2CD3B10131Jan2011 
2CD3B10128Feb2011 
2CD3B10131Mar2011 
2CD3B10130Apr2011 
2CD3B10131May2011 
2CD3B10130Jun2011 
2CD3B10131Jul2011 
2CD3B10131Aug2011 
2CD3B10130Sep2011 
2CD3B10131Oct2011 
2CD3B10130Nov2011 
2CD3B10131Dec2011 
2CD3B10131Jan2012 
2CD3B10129Feb2012 
2CD3B10131Mar2012 
2CD3B10130Apr2012 
2CD3B10131May2012 
2CD3B10130Jun2012 
2CD3B10131Jul2012 
2CD3B10131Aug2012 
2CD3B10130Sep2012 
2CD3B10131Oct2012 
2CD3B10130Nov2012 
2CD3B10131Dec2012 
2CD3B10131Jan2013 
2CD3B10128Feb2013 
2CD3B10131Mar2013 
2CD3B10130Apr2013 
2CD3B10131May2013 
2CD3B10130Jun2013 
2CD3B10131Jul2013 
2CD3B10131Aug2013 
2CD3B10130Sep2013 
2CD3B10131Oct2013 
2CD3B10130Nov2013 
2CD3B10131Dec2013 
2CD3B10131Jan2014 
2CD3B10128Feb2014 
2CD3B10131Mar2014 
2CD3B10130Apr2014 
2CD3B10131May2014 
2CD3B10130Jun2014 
2CD3B10131Jul2014 
2CD3B10131Aug2014 
2CD3B10130Sep2014 
2CD3B10131Oct2014 
2CD3B10130Nov2014 
2CD3B10131Dec2014 
2CD3B10131Jan2015 
2CD3B10128Feb2015 
2CD3B10131Mar2015 
2CD3B10130Apr2015 
2CD3B10131May2015 
2CD3B10130Jun2015 
2CD3B10131Jul2015 
2CD3B10131Aug2015 
2CD3B10130Sep2015 
2CD3B10131Oct2015 
2CD3B10130Nov2015 
2CD3B10131Dec2015 
2CD3B10131Jan2016 
2CD3B10129Feb2016 
2CD3B10131Mar2016 
2CD3B10130Apr2016 
2CD3B10131May2016 
2CD3B10130Jun2016 
2CD3B10131Jul2016 
2CD3B10131Aug2016 
2CD3B10130Sep2016 
2CD3B10131Oct2016 
2CD3B10130Nov2016 
2CD3B10131Dec2016 
2CD3B10131Jan2017 
2CD3B10128Feb2017 
2CD3B10131Mar2017 
2CD3B10131Mar2017psale
2CD3B10130Apr2017 
2CD3B10131May2017 
2CD3B10130Jun2017 
2CD3B10131Jul2017 
2CD3B10131Aug2017 
2CD3B10131Aug2017sumap
2CD3B10130Sep2017 
2CD3B10131Oct2017 
2CD3B10131Oct2017sumu
2CD3B10130Nov2017sumsv
2CD3B10130Nov2017 
2CD3B10131Dec2017 
2CD3B10131Jan2018 
2CD3B10128Feb2018 
2CD3B10128Feb2018judct
2CD3B10131Mar2018judo
2CD3B10131Mar2018 
2CD3B10130Apr2018 
2CD3B10131May2018 
2CD3B10131May2018ntva
2CD3B10130Jun2018vlreq
2CD3B10130Jun2018poss
2CD3B10130Jun2018biarr
2CD3B10130Jun2018 
2CD3B10131Jul2018 
2CD3B10131Jul2018vrec
2CD3B10131Aug2018propr
2CD3B10131Aug2018nsold
2CD3B10131Aug2018 
2CD3B10130Sep2018 
2CD3B10130Sep2018soldc
2CD3B10130Sep2018soldp
2CD3B10131Oct2018 
2CD3B10130Nov2018 
2CD3B10130Nov2018post
2CD3B10130Nov2018msset
2CD3B10130Nov2018woffa
2CD3B10130Nov2018fin
2CD3B10131Dec2018 
2CD3B10131Jan2019 
2CD3B10128Feb2019 
2CD3B10131Mar2019 
2CD3B10130Apr2019 

 

 

 

The logic I wanted to write is

 

First Step:

 

Delete the rows below oldest act_code and

Delete the rows above newest act_code

 

Need to see like below data...

 

COLL_IDNN_HKMAT_CLREFEND_OF_MONTHACT_CODE
2CD3B10131Mar2017psale
2CD3B10130Apr2017 
2CD3B10131May2017 
2CD3B10130Jun2017 
2CD3B10131Jul2017 
2CD3B10131Aug2017 
2CD3B10131Aug2017sumap
2CD3B10130Sep2017 
2CD3B10131Oct2017 
2CD3B10131Oct2017sumu
2CD3B10130Nov2017sumsv
2CD3B10130Nov2017 
2CD3B10131Dec2017 
2CD3B10131Jan2018 
2CD3B10128Feb2018 
2CD3B10128Feb2018judct
2CD3B10131Mar2018judo
2CD3B10131Mar2018 
2CD3B10130Apr2018 
2CD3B10131May2018 
2CD3B10131May2018ntva
2CD3B10130Jun2018vlreq
2CD3B10130Jun2018poss
2CD3B10130Jun2018biarr
2CD3B10130Jun2018 
2CD3B10131Jul2018 
2CD3B10131Jul2018vrec
2CD3B10131Aug2018propr
2CD3B10131Aug2018nsold
2CD3B10131Aug2018 
2CD3B10130Sep2018 
2CD3B10130Sep2018soldc
2CD3B10130Sep2018soldp
2CD3B10131Oct2018 
2CD3B10130Nov2018 
2CD3B10130Nov2018post
2CD3B10130Nov2018msset
2CD3B10130Nov2018woffa
2CD3B10130Nov2018fin

 

Secong Step:

 

Populate the missing within the value from previous month for the action code.

Final dataset should be like this...

 

COLL_IDNN_HKMAT_CLREFEND_OF_MONTHACT_CODE
2CD3B10131Mar2017psale
2CD3B10130Apr2017psale
2CD3B10131May2017psale
2CD3B10130Jun2017psale
2CD3B10131Jul2017psale
2CD3B10131Aug2017psale
2CD3B10131Aug2017sumap
2CD3B10130Sep2017sumap
2CD3B10131Oct2017sumap
2CD3B10131Oct2017sumu
2CD3B10130Nov2017sumsv
2CD3B10130Nov2017sumsv
2CD3B10131Dec2017sumsv
2CD3B10131Jan2018sumsv
2CD3B10128Feb2018sumsv
2CD3B10128Feb2018judct
2CD3B10131Mar2018judo
2CD3B10131Mar2018judo
2CD3B10130Apr2018judo
2CD3B10131May2018judo
2CD3B10131May2018ntva
2CD3B10130Jun2018vlreq
2CD3B10130Jun2018poss
2CD3B10130Jun2018biarr
2CD3B10130Jun2018biarr
2CD3B10131Jul2018biarr
2CD3B10131Jul2018vrec
2CD3B10131Aug2018propr
2CD3B10131Aug2018nsold
2CD3B10131Aug2018nsold
2CD3B10130Sep2018nsold
2CD3B10130Sep2018soldc
2CD3B10130Sep2018soldp
2CD3B10131Oct2018soldp
2CD3B10130Nov2018soldp
2CD3B10130Nov2018post
2CD3B10130Nov2018msset
2CD3B10130Nov2018woffa
2CD3B10130Nov2018fin

 

Please let me know if its not clear...thank you for helping out

 

Regards,

RS

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @BaalaRaaji   It's pretty straight forward. A sound knowledge of by group processing and joins for look up will help.

 


data have;
infile cards truncover;
input COLL_IDNN_HK $ 	MAT_CLREF	END_OF_MONTH :date9.	ACT_CODE $;
format END_OF_MONTH :date9.;
cards;
2CD3B	101	31-Jan-10	
2CD3B	101	28-Feb-10	
2CD3B	101	31-Mar-10	
2CD3B	101	30-Apr-10	
2CD3B	101	31-May-10	
2CD3B	101	30-Jun-10	
2CD3B	101	31-Jul-10	
2CD3B	101	31-Aug-10	
2CD3B	101	30-Sep-10	
2CD3B	101	31-Oct-10	
2CD3B	101	30-Nov-10	
2CD3B	101	31-Dec-10	
2CD3B	101	31-Jan-11	
2CD3B	101	28-Feb-11	
2CD3B	101	31-Mar-11	
2CD3B	101	30-Apr-11	
2CD3B	101	31-May-11	
2CD3B	101	30-Jun-11	
2CD3B	101	31-Jul-11	
2CD3B	101	31-Aug-11	
2CD3B	101	30-Sep-11	
2CD3B	101	31-Oct-11	
2CD3B	101	30-Nov-11	
2CD3B	101	31-Dec-11	
2CD3B	101	31-Jan-12	
2CD3B	101	29-Feb-12	
2CD3B	101	31-Mar-12	
2CD3B	101	30-Apr-12	
2CD3B	101	31-May-12	
2CD3B	101	30-Jun-12	
2CD3B	101	31-Jul-12	
2CD3B	101	31-Aug-12	
2CD3B	101	30-Sep-12	
2CD3B	101	31-Oct-12	
2CD3B	101	30-Nov-12	
2CD3B	101	31-Dec-12	
2CD3B	101	31-Jan-13	
2CD3B	101	28-Feb-13	
2CD3B	101	31-Mar-13	
2CD3B	101	30-Apr-13	
2CD3B	101	31-May-13	
2CD3B	101	30-Jun-13	
2CD3B	101	31-Jul-13	
2CD3B	101	31-Aug-13	
2CD3B	101	30-Sep-13	
2CD3B	101	31-Oct-13	
2CD3B	101	30-Nov-13	
2CD3B	101	31-Dec-13	
2CD3B	101	31-Jan-14	
2CD3B	101	28-Feb-14	
2CD3B	101	31-Mar-14	
2CD3B	101	30-Apr-14	
2CD3B	101	31-May-14	
2CD3B	101	30-Jun-14	
2CD3B	101	31-Jul-14	
2CD3B	101	31-Aug-14	
2CD3B	101	30-Sep-14	
2CD3B	101	31-Oct-14	
2CD3B	101	30-Nov-14	
2CD3B	101	31-Dec-14	
2CD3B	101	31-Jan-15	
2CD3B	101	28-Feb-15	
2CD3B	101	31-Mar-15	
2CD3B	101	30-Apr-15	
2CD3B	101	31-May-15	
2CD3B	101	30-Jun-15	
2CD3B	101	31-Jul-15	
2CD3B	101	31-Aug-15	
2CD3B	101	30-Sep-15	
2CD3B	101	31-Oct-15	
2CD3B	101	30-Nov-15	
2CD3B	101	31-Dec-15	
2CD3B	101	31-Jan-16	
2CD3B	101	29-Feb-16	
2CD3B	101	31-Mar-16	
2CD3B	101	30-Apr-16	
2CD3B	101	31-May-16	
2CD3B	101	30-Jun-16	
2CD3B	101	31-Jul-16	
2CD3B	101	31-Aug-16	
2CD3B	101	30-Sep-16	
2CD3B	101	31-Oct-16	
2CD3B	101	30-Nov-16	
2CD3B	101	31-Dec-16	
2CD3B	101	31-Jan-17	
2CD3B	101	28-Feb-17	
2CD3B	101	31-Mar-17	
2CD3B	101	31-Mar-17	psale
2CD3B	101	30-Apr-17	
2CD3B	101	31-May-17	
2CD3B	101	30-Jun-17	
2CD3B	101	31-Jul-17	
2CD3B	101	31-Aug-17	
2CD3B	101	31-Aug-17	sumap
2CD3B	101	30-Sep-17	
2CD3B	101	31-Oct-17	
2CD3B	101	31-Oct-17	sumu
2CD3B	101	30-Nov-17	sumsv
2CD3B	101	30-Nov-17	
2CD3B	101	31-Dec-17	
2CD3B	101	31-Jan-18	
2CD3B	101	28-Feb-18	
2CD3B	101	28-Feb-18	judct
2CD3B	101	31-Mar-18	judo
2CD3B	101	31-Mar-18	
2CD3B	101	30-Apr-18	
2CD3B	101	31-May-18	
2CD3B	101	31-May-18	ntva
2CD3B	101	30-Jun-18	vlreq
2CD3B	101	30-Jun-18	poss
2CD3B	101	30-Jun-18	biarr
2CD3B	101	30-Jun-18	
2CD3B	101	31-Jul-18	
2CD3B	101	31-Jul-18	vrec
2CD3B	101	31-Aug-18	propr
2CD3B	101	31-Aug-18	nsold
2CD3B	101	31-Aug-18	
2CD3B	101	30-Sep-18	
2CD3B	101	30-Sep-18	soldc
2CD3B	101	30-Sep-18	soldp
2CD3B	101	31-Oct-18	
2CD3B	101	30-Nov-18	
2CD3B	101	30-Nov-18	post
2CD3B	101	30-Nov-18	msset
2CD3B	101	30-Nov-18	woffa
2CD3B	101	30-Nov-18	fin
2CD3B	101	31-Dec-18	
2CD3B	101	31-Jan-19	
2CD3B	101	28-Feb-19	
2CD3B	101	31-Mar-19	
2CD3B	101	30-Apr-19	
;


proc sql;
create table temp(drop=t) as
select COLL_IDNN_HK,MAT_CLREF,ACT_CODE>' ' as t,min(END_OF_MONTH) as _min format=date9., max(END_OF_MONTH) as _max format=date9.
from have
group by COLL_IDNN_HK,MAT_CLREF, t
having t;
quit;

data want;
merge have temp;
by COLL_IDNN_HK MAT_CLREF;
if (act_code>' ' and END_OF_MONTH=_min)  or (act_code>' ' and END_OF_MONTH=_max) or
_min<END_OF_MONTH<_max;
retain _mm;
length _mm $10;
if first.MAT_CLREF then call missing(_mm);
if not missing(act_code) then _mm=act_code;
else act_code=_mm;
drop _:;
run;

View solution in original post

23 REPLIES 23
ScottBass
Rhodochrosite | Level 12

See https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... 

and repost with a self-contained "have" and "want" data steps.  Format your code using the "Insert SAS Code" icon.

 

Step 1:  Use a surrogate key (incrementing number), pre-process your data getting the min and max SK with non-missing act_code grouped by COLL_IDNN_HK, then delete rows not between min and max sk.

 

Step 2:  See https://communities.sas.com/t5/SAS-Programming/LOCF/m-p/395676/highlight/true#M95474 for LOCF examples.

 

I could have provided code instead of a word description if you'd bothered with the first step.


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
andreas_lds
Jade | Level 19

The second step can be solve by retaining an additional variable that is set to act_code and will populate act_code if it is missing. To provide something useful for the first step, i need data in usable form: a data step with datalines-statement. Maybe a retained variable can help here, too.

novinosrin
Tourmaline | Level 20

Hi @BaalaRaaji   It's pretty straight forward. A sound knowledge of by group processing and joins for look up will help.

 


data have;
infile cards truncover;
input COLL_IDNN_HK $ 	MAT_CLREF	END_OF_MONTH :date9.	ACT_CODE $;
format END_OF_MONTH :date9.;
cards;
2CD3B	101	31-Jan-10	
2CD3B	101	28-Feb-10	
2CD3B	101	31-Mar-10	
2CD3B	101	30-Apr-10	
2CD3B	101	31-May-10	
2CD3B	101	30-Jun-10	
2CD3B	101	31-Jul-10	
2CD3B	101	31-Aug-10	
2CD3B	101	30-Sep-10	
2CD3B	101	31-Oct-10	
2CD3B	101	30-Nov-10	
2CD3B	101	31-Dec-10	
2CD3B	101	31-Jan-11	
2CD3B	101	28-Feb-11	
2CD3B	101	31-Mar-11	
2CD3B	101	30-Apr-11	
2CD3B	101	31-May-11	
2CD3B	101	30-Jun-11	
2CD3B	101	31-Jul-11	
2CD3B	101	31-Aug-11	
2CD3B	101	30-Sep-11	
2CD3B	101	31-Oct-11	
2CD3B	101	30-Nov-11	
2CD3B	101	31-Dec-11	
2CD3B	101	31-Jan-12	
2CD3B	101	29-Feb-12	
2CD3B	101	31-Mar-12	
2CD3B	101	30-Apr-12	
2CD3B	101	31-May-12	
2CD3B	101	30-Jun-12	
2CD3B	101	31-Jul-12	
2CD3B	101	31-Aug-12	
2CD3B	101	30-Sep-12	
2CD3B	101	31-Oct-12	
2CD3B	101	30-Nov-12	
2CD3B	101	31-Dec-12	
2CD3B	101	31-Jan-13	
2CD3B	101	28-Feb-13	
2CD3B	101	31-Mar-13	
2CD3B	101	30-Apr-13	
2CD3B	101	31-May-13	
2CD3B	101	30-Jun-13	
2CD3B	101	31-Jul-13	
2CD3B	101	31-Aug-13	
2CD3B	101	30-Sep-13	
2CD3B	101	31-Oct-13	
2CD3B	101	30-Nov-13	
2CD3B	101	31-Dec-13	
2CD3B	101	31-Jan-14	
2CD3B	101	28-Feb-14	
2CD3B	101	31-Mar-14	
2CD3B	101	30-Apr-14	
2CD3B	101	31-May-14	
2CD3B	101	30-Jun-14	
2CD3B	101	31-Jul-14	
2CD3B	101	31-Aug-14	
2CD3B	101	30-Sep-14	
2CD3B	101	31-Oct-14	
2CD3B	101	30-Nov-14	
2CD3B	101	31-Dec-14	
2CD3B	101	31-Jan-15	
2CD3B	101	28-Feb-15	
2CD3B	101	31-Mar-15	
2CD3B	101	30-Apr-15	
2CD3B	101	31-May-15	
2CD3B	101	30-Jun-15	
2CD3B	101	31-Jul-15	
2CD3B	101	31-Aug-15	
2CD3B	101	30-Sep-15	
2CD3B	101	31-Oct-15	
2CD3B	101	30-Nov-15	
2CD3B	101	31-Dec-15	
2CD3B	101	31-Jan-16	
2CD3B	101	29-Feb-16	
2CD3B	101	31-Mar-16	
2CD3B	101	30-Apr-16	
2CD3B	101	31-May-16	
2CD3B	101	30-Jun-16	
2CD3B	101	31-Jul-16	
2CD3B	101	31-Aug-16	
2CD3B	101	30-Sep-16	
2CD3B	101	31-Oct-16	
2CD3B	101	30-Nov-16	
2CD3B	101	31-Dec-16	
2CD3B	101	31-Jan-17	
2CD3B	101	28-Feb-17	
2CD3B	101	31-Mar-17	
2CD3B	101	31-Mar-17	psale
2CD3B	101	30-Apr-17	
2CD3B	101	31-May-17	
2CD3B	101	30-Jun-17	
2CD3B	101	31-Jul-17	
2CD3B	101	31-Aug-17	
2CD3B	101	31-Aug-17	sumap
2CD3B	101	30-Sep-17	
2CD3B	101	31-Oct-17	
2CD3B	101	31-Oct-17	sumu
2CD3B	101	30-Nov-17	sumsv
2CD3B	101	30-Nov-17	
2CD3B	101	31-Dec-17	
2CD3B	101	31-Jan-18	
2CD3B	101	28-Feb-18	
2CD3B	101	28-Feb-18	judct
2CD3B	101	31-Mar-18	judo
2CD3B	101	31-Mar-18	
2CD3B	101	30-Apr-18	
2CD3B	101	31-May-18	
2CD3B	101	31-May-18	ntva
2CD3B	101	30-Jun-18	vlreq
2CD3B	101	30-Jun-18	poss
2CD3B	101	30-Jun-18	biarr
2CD3B	101	30-Jun-18	
2CD3B	101	31-Jul-18	
2CD3B	101	31-Jul-18	vrec
2CD3B	101	31-Aug-18	propr
2CD3B	101	31-Aug-18	nsold
2CD3B	101	31-Aug-18	
2CD3B	101	30-Sep-18	
2CD3B	101	30-Sep-18	soldc
2CD3B	101	30-Sep-18	soldp
2CD3B	101	31-Oct-18	
2CD3B	101	30-Nov-18	
2CD3B	101	30-Nov-18	post
2CD3B	101	30-Nov-18	msset
2CD3B	101	30-Nov-18	woffa
2CD3B	101	30-Nov-18	fin
2CD3B	101	31-Dec-18	
2CD3B	101	31-Jan-19	
2CD3B	101	28-Feb-19	
2CD3B	101	31-Mar-19	
2CD3B	101	30-Apr-19	
;


proc sql;
create table temp(drop=t) as
select COLL_IDNN_HK,MAT_CLREF,ACT_CODE>' ' as t,min(END_OF_MONTH) as _min format=date9., max(END_OF_MONTH) as _max format=date9.
from have
group by COLL_IDNN_HK,MAT_CLREF, t
having t;
quit;

data want;
merge have temp;
by COLL_IDNN_HK MAT_CLREF;
if (act_code>' ' and END_OF_MONTH=_min)  or (act_code>' ' and END_OF_MONTH=_max) or
_min<END_OF_MONTH<_max;
retain _mm;
length _mm $10;
if first.MAT_CLREF then call missing(_mm);
if not missing(act_code) then _mm=act_code;
else act_code=_mm;
drop _:;
run;
BaalaRaaji
Quartz | Level 8

Hi novinosrin,

Super User...Thank you so much for the code. Much Appreciated..

Brilliant thinking in replicated the code..Awesome.

 

You are a SAS Expert.thanks again

 

Cheers!

BaalaRaaji
Quartz | Level 8

The code is working properly but when I have added new variable(act_alloc_date)..its not working..?

I have added this feild because in a month I have 4 different act_code and need the last one to order..

would you be able to advise? appreciate your help.thanks

BaalaRaaji
Quartz | Level 8

Below code ...some how not working if I add act_alloc(date)

/* Base data */

proc sql;

create table test as select a.coll_idnn_hk,a.MAT_CLREF ,a.END_OF_MONTH,b.ACT_CODE,b.act_alloc

from HK_MONTH_DAT as a

left join com_HK as b on

a.coll_idnn_hk=b.coll_idnn_hk and a.END_OF_MONTH= b.END_OF_MONTH

where MAT_CLREF ne ' '

order by a.COLL_IDNN_HK,a.MAT_CLREF,b.act_alloc;

quit;

 

/**/

/*proc sort data=test out= x1;by coll_idnn_hk MAT_CLREF act_alloc ;run; */

 

proc sql;

create table temp(drop=t) as

select COLL_IDNN_HK,MAT_CLREF,ACT_CODE>' ' as t,min(END_OF_MONTH) as _min format=date9., max(END_OF_MONTH) as _max format=date9.

from test

group by COLL_IDNN_HK,MAT_CLREF,t

having t;

quit;

data want;

merge test temp;

by COLL_IDNN_HK MAT_CLREF ;

 

/*if (act_code>' ' and END_OF_MONTH=_min) or (act_code>' ' and END_OF_MONTH=_max) or*/

/*_min<END_OF_MONTH<_max;*/

retain _mm;

length _mm $10;

if first.MAT_CLREF then call missing(_mm);

if not missing(act_code) then _mm=act_code;

else act_code=_mm;

drop _:;

run;

proc sort data=want1 out= c1; by coll_idnn_hk end_of_month act_alloc act_code ;run;

 

novinosrin
Tourmaline | Level 20

Hi @BaalaRaaji   Can you hang in there for a night as it is 9:20pm chicago time. Let me test in the morning and message you back.  

BaalaRaaji
Quartz | Level 8

Hey, Sorry to bother and distribut you..Am from Australia and was working to finialise this code..

please don't worry as the code is working perfectly..

 

Thanks and Reagrds,

RS

novinosrin
Tourmaline | Level 20

Always happy to help and feel free. Just that I do not have access to SAS software at home and so the reason in not being able to help anybody. So in future, weekends at nights are a diffiicult  as my typical routine is friends, restaurants and other chores, otherwise SAS is always fun. 

 

PS

Nonetheless i'm always online here on SAS communities on my mobile phone

BaalaRaaji
Quartz | Level 8

Appreciate your help. Definitely will point you directly if I stuck with the logics.

 

thank you 

BaalaRaaji
Quartz | Level 8

sorry, Ignore it..i have fixed it..

your code is still working..thanks a lot..Appreciate it..

 

Chees

RS

Shmuel
Garnet | Level 18

Assuming data set is sorted by Coll_IDNN_HK  MAT_CLREF END_OF_MONTH , then

 

you can achieve what you want by few steps:

      

data temp1;
  set have;
       retain flag 0;
       if flag=0 and missing(act_code) then delete;
       else flag=1;
run;

proc sort data=temp1;
  by Coll_IDNN_HK  MAT_CLREF descending END_OF_MONTH;
run;

data temp2;
 set temp1;
       retain flag 0;
       if flag=0 and missing(act_code) then delete;
       else flag=1;
run;

proc sort data=temp2;
  by Coll_IDNN_HK  MAT_CLREF END_OF_MONTH;
run;

data want;
 set temp2;
       retain prev_code;
       if not missing(act_code)
       then prev_code = act_code;
       else act_code = prev_code;
run;


 

 

 

 

 

 

BaalaRaaji
Quartz | Level 8

Thanks Shmuel..Much Appreciated...

 

 

ScottBass
Rhodochrosite | Level 12

Here is the approach I was suggesting.  I get a slightly different result than @novinosrin (obs 35, soldp).

 

Assuming my business logic is correct, I find my code a bit easier to follow (but of course everyone would think their own code is easier to follow - that's just natural).  The big question is which code is creating the correct result?

 

data have;
infile cards truncover;
input COLL_IDNN_HK $ MAT_CLREF END_OF_MONTH :date9. ACT_CODE $;
format END_OF_MONTH :date9.;
cards;
2CD3B   101   31-Jan-10
2CD3B   101   28-Feb-10
2CD3B   101   31-Mar-10
2CD3B   101   30-Apr-10
2CD3B   101   31-May-10
2CD3B   101   30-Jun-10
2CD3B   101   31-Jul-10
2CD3B   101   31-Aug-10
2CD3B   101   30-Sep-10
2CD3B   101   31-Oct-10
2CD3B   101   30-Nov-10
2CD3B   101   31-Dec-10
2CD3B   101   31-Jan-11
2CD3B   101   28-Feb-11
2CD3B   101   31-Mar-11
2CD3B   101   30-Apr-11
2CD3B   101   31-May-11
2CD3B   101   30-Jun-11
2CD3B   101   31-Jul-11
2CD3B   101   31-Aug-11
2CD3B   101   30-Sep-11
2CD3B   101   31-Oct-11
2CD3B   101   30-Nov-11
2CD3B   101   31-Dec-11
2CD3B   101   31-Jan-12
2CD3B   101   29-Feb-12
2CD3B   101   31-Mar-12
2CD3B   101   30-Apr-12
2CD3B   101   31-May-12
2CD3B   101   30-Jun-12
2CD3B   101   31-Jul-12
2CD3B   101   31-Aug-12
2CD3B   101   30-Sep-12
2CD3B   101   31-Oct-12
2CD3B   101   30-Nov-12
2CD3B   101   31-Dec-12
2CD3B   101   31-Jan-13
2CD3B   101   28-Feb-13
2CD3B   101   31-Mar-13
2CD3B   101   30-Apr-13
2CD3B   101   31-May-13
2CD3B   101   30-Jun-13
2CD3B   101   31-Jul-13
2CD3B   101   31-Aug-13
2CD3B   101   30-Sep-13
2CD3B   101   31-Oct-13
2CD3B   101   30-Nov-13
2CD3B   101   31-Dec-13
2CD3B   101   31-Jan-14
2CD3B   101   28-Feb-14
2CD3B   101   31-Mar-14
2CD3B   101   30-Apr-14
2CD3B   101   31-May-14
2CD3B   101   30-Jun-14
2CD3B   101   31-Jul-14
2CD3B   101   31-Aug-14
2CD3B   101   30-Sep-14
2CD3B   101   31-Oct-14
2CD3B   101   30-Nov-14
2CD3B   101   31-Dec-14
2CD3B   101   31-Jan-15
2CD3B   101   28-Feb-15
2CD3B   101   31-Mar-15
2CD3B   101   30-Apr-15
2CD3B   101   31-May-15
2CD3B   101   30-Jun-15
2CD3B   101   31-Jul-15
2CD3B   101   31-Aug-15
2CD3B   101   30-Sep-15
2CD3B   101   31-Oct-15
2CD3B   101   30-Nov-15
2CD3B   101   31-Dec-15
2CD3B   101   31-Jan-16
2CD3B   101   29-Feb-16
2CD3B   101   31-Mar-16
2CD3B   101   30-Apr-16
2CD3B   101   31-May-16
2CD3B   101   30-Jun-16
2CD3B   101   31-Jul-16
2CD3B   101   31-Aug-16
2CD3B   101   30-Sep-16
2CD3B   101   31-Oct-16
2CD3B   101   30-Nov-16
2CD3B   101   31-Dec-16
2CD3B   101   31-Jan-17
2CD3B   101   28-Feb-17
2CD3B   101   31-Mar-17
2CD3B   101   31-Mar-17   psale
2CD3B   101   30-Apr-17
2CD3B   101   31-May-17
2CD3B   101   30-Jun-17
2CD3B   101   31-Jul-17
2CD3B   101   31-Aug-17
2CD3B   101   31-Aug-17   sumap
2CD3B   101   30-Sep-17
2CD3B   101   31-Oct-17
2CD3B   101   31-Oct-17   sumu
2CD3B   101   30-Nov-17   sumsv
2CD3B   101   30-Nov-17
2CD3B   101   31-Dec-17
2CD3B   101   31-Jan-18
2CD3B   101   28-Feb-18
2CD3B   101   28-Feb-18   judct
2CD3B   101   31-Mar-18   judo
2CD3B   101   31-Mar-18
2CD3B   101   30-Apr-18
2CD3B   101   31-May-18
2CD3B   101   31-May-18   ntva
2CD3B   101   30-Jun-18   vlreq
2CD3B   101   30-Jun-18   poss
2CD3B   101   30-Jun-18   biarr
2CD3B   101   30-Jun-18
2CD3B   101   31-Jul-18
2CD3B   101   31-Jul-18   vrec
2CD3B   101   31-Aug-18   propr
2CD3B   101   31-Aug-18   nsold
2CD3B   101   31-Aug-18
2CD3B   101   30-Sep-18
2CD3B   101   30-Sep-18   soldc
2CD3B   101   30-Sep-18   soldp
2CD3B   101   31-Oct-18
2CD3B   101   30-Nov-18
2CD3B   101   30-Nov-18   post
2CD3B   101   30-Nov-18   msset
2CD3B   101   30-Nov-18   woffa
2CD3B   101   30-Nov-18   fin
2CD3B   101   31-Dec-18
2CD3B   101   31-Jan-19
2CD3B   101   28-Feb-19
2CD3B   101   31-Mar-19
2CD3B   101   30-Apr-19
;
run;

* my approach ;
data sk / view=sk;
   sk+1;
   set have;
run;

proc sql;
   create table min_max as
   select COLL_IDNN_HK, min(sk) as min_sk, max(sk) as max_sk
   from sk
   where ACT_CODE is not missing
   group by COLL_IDNN_HK;

   create table want as
   select a.*
   from sk a
   inner join min_max b
   on a.COLL_IDNN_HK=b.COLL_IDNN_HK
   where a.sk between min_sk and max_sk;
quit;

* last observation carried forward (LOCF) ;
data want;
   update want (obs=0) want;
   by COLL_IDNN_HK;
   output;
   drop sk;
run;

* @novinosrin's approach ;
proc sql;
create table temp(drop=t) as
select COLL_IDNN_HK,MAT_CLREF,ACT_CODE>' ' as t,min(END_OF_MONTH) as _min format=date9., max(END_OF_MONTH) as _max format=date9.
from have
group by COLL_IDNN_HK,MAT_CLREF, t
having t;
quit;

data want2;
merge have temp;
by COLL_IDNN_HK MAT_CLREF;
if (act_code>' ' and END_OF_MONTH=_min)  or (act_code>' ' and END_OF_MONTH=_max) or
_min<END_OF_MONTH<_max;
retain _mm;
length _mm $10;
if first.MAT_CLREF then call missing(_mm);
if not missing(act_code) then _mm=act_code;
else act_code=_mm;
drop _:;
run;

proc compare base=want2 comp=want;
run;

 


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.

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
  • 23 replies
  • 2716 views
  • 5 likes
  • 5 in conversation