BookmarkSubscribeRSS Feed
mlogan
Lapis Lazuli | Level 10

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

 

5 REPLIES 5
art297
Opal | Level 21

Here is one way:

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

Art, CEO, AnalystFinder.com

 

mlogan
Lapis Lazuli | Level 10
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,
art297
Opal | Level 21

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

 

Art, CEO, AnalystFinder.com

 

 

Reeza
Super User

@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.

 

ChrisBrooks
Ammonite | Level 13

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1446 views
  • 0 likes
  • 4 in conversation