Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting modified rows based on variable

Status
Not open for further replies.

Tamalam

Technical User
Dec 30, 2008
2
CA
Does anyone know how to expand a dataset? I have been struggling for weeks now - can't figure this one out - hope there is someone out there who can help:

Original dataset:

Patient Date Days
TJones Sept.1/08 3

Desired dataset:

Patient Date Days
TJones Sept.1/08 1
TJones Sept.2/08 1
TJones Sept.3/08 1

So I need to identify those patients who stayed more than one day (where variable Days >1) and then, duplicate that record and modify the date and the days. The goal is to have one row per day. And the original data set is very large.

Any help would be appreciated.
 
This should get you pretty close.

Code:
data have;
  input Patient $ Date date9. Days ;
  format Date date9.;
  datalines;
TJones	01SEP08	3
DSmith	01JAN08	5
;
run;

Data want;
set have;
do i=1 to days;
[tab]output; 
[tab]Date +1;
end;
run;

The "do i=1 to days;" line is telling SAS to repeat the lines between the do and end until i matches the number in the days column. In this example, I added 1 day to Date column for each new row, but you can change that to whatever interval you need.

Dataset returned:
Patient Date Days i
TJones 01SEP2008 3 1
TJones 02SEP2008 3 2
TJones 03SEP2008 3 3
DSmith 01JAN2008 5 1
DSmith 02JAN2008 5 2
DSmith 03JAN2008 5 3
DSmith 04JAN2008 5 4
DSmith 05JAN2008 5 5

~~Dave
 
Dave - I don't know how to thank you enough!

Your code worked perfectly!

Thank you thank you thank you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top