Dear all,
I have the following data set, a set of entities, for each I have assigned an Indicator with the value 1, depending on the Period:
Entity Period Indicator
A 31DEC2014
A 30MAR2015 1
A 30JUN2015
A 30SEP2015
B 31DEC2014 1
B 31MAR2015
B 30JUN2015
C 31DEC2014
C 31MAR2015
C 30JUN2015 1
C 30SEP2015
D 31MAR2015 1
D 30JUN2015
I want to replicate, for subsequent periods and for entiy, the indicator assignment 1, when missing, leaving the previous periods for entity, empty. Shoud look like the following:
Entity Period Indicator
A 31DEC2014
A 30MAR2015 1
A 30JUN2015 1
A 30SEP2015 1
B 31DEC2014 1
B 31MAR2015 1
B 30JUN2015 1
C 31DEC2014
C 31MAR2015
C 30JUN2015 1
C 30SEP2015 1
D 31MAR2015 1
D 30JUN2015 1
Any hint on how to solve this issue?
Thank you
RETAIN lets you keep values from observation to observation. FIRST processing when using By group allows conditional assignment based on start of a group of related records.
This works for your example data:
data have;
informat Entity $4. period date9. indicator best4.;
format period date9.;
input Entity Period Indicator ;
datalines;
A 31DEC2014 .
A 30MAR2015 1
A 30JUN2015 .
A 30SEP2015 .
B 31DEC2014 1
B 31MAR2015 .
B 30JUN2015 .
C 31DEC2014 .
C 31MAR2015 .
C 30JUN2015 1
C 30SEP2015 .
D 31MAR2015 1
D 30JUN2015 .
;
run;
data want;
set have;
by notsorted entity;
Retain LastIndicator;
if first.entity then LastIndicator=.;
if indicator ne . then LastIndicator=indicator;
else if indicator=. then Indicator=LastIndicator;
drop LastIndicator;
run;
RETAIN lets you keep values from observation to observation. FIRST processing when using By group allows conditional assignment based on start of a group of related records.
This works for your example data:
data have;
informat Entity $4. period date9. indicator best4.;
format period date9.;
input Entity Period Indicator ;
datalines;
A 31DEC2014 .
A 30MAR2015 1
A 30JUN2015 .
A 30SEP2015 .
B 31DEC2014 1
B 31MAR2015 .
B 30JUN2015 .
C 31DEC2014 .
C 31MAR2015 .
C 30JUN2015 1
C 30SEP2015 .
D 31MAR2015 1
D 30JUN2015 .
;
run;
data want;
set have;
by notsorted entity;
Retain LastIndicator;
if first.entity then LastIndicator=.;
if indicator ne . then LastIndicator=indicator;
else if indicator=. then Indicator=LastIndicator;
drop LastIndicator;
run;
Perfect! Thank you very much for your hints! 🙂
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 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.