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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

2 REPLIES 2
Haikuo
Onyx | Level 15

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

Fisher
Quartz | Level 8

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

Thanks Hai.kuo.

sas-innovate-2024.png

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.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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