Hi,
I have a data set like this:
data trend;
input tag $ @@;
datalines;
tr-c . . . tr-c tr-tr . .
;
run;
In this data set, I like to assign the missing tag values as the previous value except that after the tag value become the 'tr-tr', all the following missing values should be 'tr'. To make it clear, I like the final data set look like this:
data trend;
input tag $ @@;
datalines;
tr-c tr-c tr-c tr-c tr-c tr-tr tr tr
;
run;
This can be implemented using retain statement and lag function, but I think there should be some smarter way to implement it.
Thanks.
Try this:
data trend;
input tag $ @@;
length _tag $8;
retain _tag ;
_tag=coalescec(tag,_tag);
if missing(tag) then tag=ifc(_tag='tr-tr','tr',_tag);
drop _tag;
datalines;
tr-c . . . tr-c tr-tr . .
;
run;
Haikuo
Try this:
data trend;
input tag $ @@;
length _tag $8;
retain _tag ;
_tag=coalescec(tag,_tag);
if missing(tag) then tag=ifc(_tag='tr-tr','tr',_tag);
drop _tag;
datalines;
tr-c . . . tr-c tr-tr . .
;
run;
Haikuo
Excellent! I never tried this function, always learn something from SAS gurus here.
Thanks Hai.kuo.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.