DATA Step, Macro, Functions and more

How to fill with repetitive value

Reply
Regular Contributor
Posts: 218

How to fill with repetitive value

Hi All,

I am trying to fill the below cells with repetitive value. Can someone help me please

 

My Table (table name is have)

 

State  Name

NY     Joy

          Thomas

          Mike

TX     Albert

         James

IL      Corey

 

Output Table (Table name is want):

 

State  Name        STATE2

NY     Joy             NY

          Thomas      NY

          Mike           NY

TX     Albert         TX

         James         TX

IL      Corey          IL

 

PROC Star
Posts: 7,492

Re: How to fill with repetitive value

Here is one way:

data want;
  set have;
  retain state2;
  if not missing(state) then state2=state;
run;

Art, CEO, AnalystFinder.com

 

Regular Contributor
Posts: 218

Re: How to fill with repetitive value

Hi art297, Your code just copy the column STATE to STATE2. Would you please verify. Remember that STATE2 variable did not exist in the have database.

Thanks,
PROC Star
Posts: 7,492

Re: How to fill with repetitive value

Run the code on your data. It does what I think you requested.

 

Art, CEO, AnalystFinder.com

 

 

Super User
Posts: 19,878

Re: How to fill with repetitive value


mlogan wrote:
Hi art297, Your code just copy the column STATE to STATE2. 

No it doesn't. 

 

The code is:

data want;
  set have;
  retain state2;
  if not missing(state) then state2=state;
run;

 

RETAIN holds the value until it is changed explicitly.

The assignment of State to STATE2 only occurs when there is a new value of STATE.  It returns exactly your desired stated output using the simplest methods.

 

If it isn't doing what you want, you need to provide more details.

 

Super Contributor
Posts: 441

Re: How to fill with repetitive value

This should do the trick

 

data have;
	infile datalines dlm="," missover truncover;
	length state $2 name $6;
	input state $ name $;
	datalines;
NY,Joy
   ,Thomas
   ,Mike
TX,Albert
   ,James
IL,Corey
;
run;

data want(drop=holdstate);
	length state2 $2;
	set have;
	if _n_=1 then do;
		holdstate=state;
		state2=state;
	end;
	else do;
		if state="" then state2=holdstate;
		else do;
			holdstate=state;
			state2=state;
		end;
	end;
	retain holdstate;
run;
Ask a Question
Discussion stats
  • 5 replies
  • 133 views
  • 0 likes
  • 4 in conversation