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

Hello everyone,

 

I have a dataset that looks like this:

 

IDDateDays
120JAN20115
13FEB20114
110MAR20111
28AUG201110
312DEC201129
45JAN201321
51 MAR201414

 

 

I want to add new observations by ID based on the number of days. How do I get this output?

IDDateDays
120JAN20115
121JAN20115
122JAN20115
123JAN20115
124JAN20115
125JAN20115
13FEB20114
14FEB20114
15FEB20114
16FEB20114

 

and so on.....

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Works for me:

 

data have;
input ID  Date :date9.   Days;
format date yymmdd10.;
datalines;
1   20JAN2011   5
1   3FEB2011    4
1   10MAR2011   1
2   8AUG2011    10
3   12DEC2011   29
4   5JAN2013    21
5   1MAR2014   14
;

data want;
set have;
do i = 1 to days;
    output;
    date = intnx("day", date, 1);
    end;
drop i;
run;

proc print noobs data=want; run;

PGStats_0-1608845765436.png

....

PG

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

Are your dates actual numeric SAS dates? Or are they character strings?

 

Why does you output have 6 records from the first observation and 4 records from the second observation?

--
Paige Miller
raajdesaii
Fluorite | Level 6

They are actual numeric SAS dates. 

 

The first patient has 6 records where as the second patient only has 4 records and so on. Date is the variable which indicates when they started the drug and days indicates for how many dates. So patient 1 started the first treatment  on a particular date for x number of days. Same patient started another treatment on another date for x number of days. I just want to add all the days to the date so I get the dataset as I have posted.

PaigeMiller
Diamond | Level 26

If the first patient had 5 records in your output, and the second patient had 4 records in your output, then your explanation makes sense because days for the first patient is 5 and days for the second patient has 4.

 

But that's not what your output shows. Please explain why the first patient has 6 and the second patient has 4.

--
Paige Miller
raajdesaii
Fluorite | Level 6

My bad, that is just an error.

 

IDDateDays
120JAN20115
121JAN20115
122JAN20115
123JAN20115
124JAN20115
13FEB20114
14FEB20114
15FEB20114
16FEB20114
PGStats
Opal | Level 21
data want;
set have;
do i = 1 to days;
    output;
    date = intnx("day", date, 1);
    end;
drop i;
run;
PG
raajdesaii
Fluorite | Level 6
I tried using this but it did not work, it would add the same date to all the id's. I want it to look like this:

ID Date Days
1 20JAN2011 5
1 21JAN2011 5
1 22JAN2011 5
1 23JAN2011 5
1 24JAN2011 5
1 3FEB2011 4
1 4FEB2011 4
1 5FEB2011 4
1 6FEB2011 4
PGStats
Opal | Level 21

Works for me:

 

data have;
input ID  Date :date9.   Days;
format date yymmdd10.;
datalines;
1   20JAN2011   5
1   3FEB2011    4
1   10MAR2011   1
2   8AUG2011    10
3   12DEC2011   29
4   5JAN2013    21
5   1MAR2014   14
;

data want;
set have;
do i = 1 to days;
    output;
    date = intnx("day", date, 1);
    end;
drop i;
run;

proc print noobs data=want; run;

PGStats_0-1608845765436.png

....

PG
raajdesaii
Fluorite | Level 6

That worked! I had made a small error. Thanks!

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
  • 8 replies
  • 1770 views
  • 0 likes
  • 3 in conversation