BookmarkSubscribeRSS Feed
hwangnyc
Quartz | Level 8

Hi everyone,

 

This is what I have:

PeriodStaffID
StartJohn1234
End 1234
End 1234
End 1234
StartDira9876
End 9876

 

and this is what I want:

PeriodStaffID
StartJohn1234
EndJohn1234
EndJohn1234
EndJohn1234
StartDira9876
EndDira9876
3 REPLIES 3
novinosrin
Tourmaline | Level 20

 


data have;
input Period $	Staff $	ID;
cards;
Start	John	1234
End	.	 	1234
End	. 	1234
End	 .	1234
Start	Dira	9876
End	 	. 9876
;
data want;

update have(obs=0) have;
by id; output; run;

 

r_behata
Barite | Level 11
data have;
input Period $	Staff $ ID $;
infile cards dsd;
cards;
Start,John,1234
End,,1234
End,,1234
End,,1234
Start,Dira,9876
End,,9876
;
run;


data want;
	retain Staff_;
	set have;
	by id  ;
	if first.id then Staff_=Staff;
	else if 	missing(staff) then staff=staff_;

	drop Staff_;
run;
mkeintz
PROC Star

If the UPDATE statement offered by @novinosrin seems slightly opaque, there is another way to avoid using RETAIN as per @r_behata.  You can utilize the fact that any variable read by a SET is automatically retained until that variable is explicitly overwritten.  So below the STAFF variable is only read in when period='Start', and therefore will be retained until the next period='Start' by executing the SET statement conditionally (i.e. "IF period='Start' then set ...").

 

data have;
input Period $	Staff $ ID $;
infile cards dsd;
cards;
Start,John,1234
End,,1234
End,,1234
End,,1234
Start,Dira,9876
End,,9876
;
run;
data want;
  set have (drop=staff);
  if period='Start' then set have (keep=staff) point=_n_;
run;

 

But remember one thing - while variable STAFF is the only variable read in by the conditional SET statement, it is also dropped from the unconditional SET.

 

Now if you want to propagate ALL variables forward to replace missing values across an ID, use @novinosrin's UPDATE suggestion.  But if you only wish to do it for a particular subset of variables, try the above.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

SAS Innovate 2025: Call for Content

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 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 609 views
  • 1 like
  • 4 in conversation