DATA Step, Macro, Functions and more

What is the best way to implement this logic?

Accepted Solution Solved
Reply
Contributor
Posts: 70
Accepted Solution

What is the best way to implement this logic?

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.


Accepted Solutions
Solution
‎11-09-2012 02:58 PM
Respected Advisor
Posts: 3,124

Re: What is the best way to implement this logic?

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

View solution in original post


All Replies
Solution
‎11-09-2012 02:58 PM
Respected Advisor
Posts: 3,124

Re: What is the best way to implement this logic?

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

Contributor
Posts: 70

Re: What is the best way to implement this logic?

Excellent! I never tried this function, always learn something from SAS gurus here.

Thanks Hai.kuo.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 201 views
  • 0 likes
  • 2 in conversation