BookmarkSubscribeRSS Feed
MV04
Obsidian | Level 7

I have got a dataset as below and I need to loop through the dates increaed by 1 in each by group of ID


data have;
format eff_dat date9. ;
infile cards dlm = ',' ;
input eff_dat date9. id amt;
cards;
30dec2008,1221,20
01feb2009,1221,30
01mar2009,1222,10
01apr2009,1222,15
;run;

 I need the result as below

30-Dec-08122120
31-Dec-08122120
01-Jan-09122120
02-Jan-09122120
03-Jan-09122120
04-Jan-09122120
05-Jan-09122120
06-Jan-09122120
07-Jan-09122120
08-Jan-09122120
09-Jan-09122120
10-Jan-09122120
11-Jan-09122120
12-Jan-09122120
13-Jan-09122120
14-Jan-09122120
15-Jan-09122120
16-Jan-09122120
17-Jan-09122120
18-Jan-09122120
19-Jan-09122120
20-Jan-09122120
21-Jan-09122120
22-Jan-09122120
23-Jan-09122120
24-Jan-09122120
25-Jan-09122120
26-Jan-09122120
27-Jan-09122120
28-Jan-09122120
29-Jan-09122120
30-Jan-09122120
31-Jan-09122120
01-Feb-09122130
02-Feb-09122130
03-Feb-09122130
04-Feb-09122130
05-Feb-09122130
06-Feb-09122130
07-Feb-09122130
08-Feb-09122130
09-Feb-09122130
10-Feb-09122130
11-Feb-09122130
12-Feb-09122130
13-Feb-09122130
14-Feb-09122130
15-Feb-09122130
16-Feb-09122130
17-Feb-09122130
18-Feb-09122130
19-Feb-09122130
20-Feb-09122130
21-Feb-09122130
22-Feb-09122130
23-Feb-09122130
24-Feb-09122130
25-Feb-09122130
26-Feb-09122130
27-Feb-09122130
28-Feb-09122130
01-Mar-09122210
02-Mar-09122210
03-Mar-09122210
04-Mar-09122210
05-Mar-09122210
06-Mar-09122210
07-Mar-09122210
08-Mar-09122210
09-Mar-09122210
10-Mar-09122210
11-Mar-09122210
12-Mar-09122210
13-Mar-09122210
14-Mar-09122210
15-Mar-09122210
16-Mar-09122210
17-Mar-09122210
18-Mar-09122210
19-Mar-09122210
20-Mar-09122210
21-Mar-09122210
22-Mar-09122210
23-Mar-09122210
24-Mar-09122210
25-Mar-09122210
26-Mar-09122210
27-Mar-09122210
28-Mar-09122210
29-Mar-09122210
30-Mar-09122210
31-Mar-09122210
13 REPLIES 13
ballardw
Super User

I don't see a BY group anywhere.

Is your actual requirement to create additional records looking ahead such as from 30Dec to one day before the start of the date on the next record?

PeterClemmensen
Tourmaline | Level 20

Do someting like this

 

data have;
format eff_dat date9. ;
infile cards dlm = ',' ;
input eff_dat date9. id amt;
cards;
30dec2008,1221,20
01feb2009,1221,30
01mar2009,1222,10
01apr2009,1222,15
;run;

data want(keep=date id amt);
   merge have end=lr
         have(firstobs=2 keep=eff_dat rename = (eff_dat=lead_dat));

   if not (lr) then do date=eff_dat to lead_dat-1;
      output;
   end;

   format date date9.;
run;
MV04
Obsidian | Level 7

Thanks for the reply Ballardw.

but it seemlike it is not switching on to the next BY group so the result is stopping at the 1st BY grup (1221)

1221 30 18FEB2009
1221 30 19FEB2009
1221 30 20FEB2009
1221 30 21FEB2009
1221 30 22FEB2009
1221 30 23FEB2009
1221 30 24FEB2009
1221 30 25FEB2009
1221 30 26FEB2009
1221 30 27FEB2009
1221 30 28FEB2009

 

Nothing after the above

MV04
Obsidian | Level 7

Apologies Draycut.

The above reply ment for you.

PeterClemmensen
Tourmaline | Level 20

No problem 🙂

 

Did you run my exact code? When I run it, I get 92 observations. The exact 92 observations you post in your question...

 

 

Capture.PNG

MV04
Obsidian | Level 7

Hi Draycut,

This is what I am saying that loop is stopping on first BY varibale and not going any futher.

We needs to get the other dates as below for other BY group as well.

so for 01mar2009 it should show 1222 10 that row is missng

PeterClemmensen
Tourmaline | Level 20

No. It is not missing. It is there, exactly as you desire

 

 

Capture.PNG

yabwon
Onyx | Level 15

Hi,

 

maybe try something like this:

 

data have;
format eff_dat date9. ;
infile cards dlm = ',' ;
input eff_dat date9. id amt;
cards;
30dec2008,1221,20
01feb2009,1221,30
01mar2009,1222,10
01apr2009,1222,15
;
run;


data want;
  loop =  eff_dat; drop loop tmp;
  put _all_;

  set have;

  tmp = eff_dat;
  if loop ne . then
    do _N_ = loop to eff_dat-1;
      eff_dat = _N_;
      output;
    end;
  eff_dat = tmp;

run;

all the best

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



MV04
Obsidian | Level 7

Hi Yabwon

I should get amt as 20 for the below

30DEC2008 1221 30
31DEC2008 1221 30
01JAN2009 1221 30
02JAN2009 1221 30
03JAN2009 1221 30
04JAN2009 1221 30
05JAN2009 1221 30
06JAN2009 1221 30
07JAN2009 1221 30
08JAN2009 1221 30
09JAN2009 1221 30
10JAN2009 1221 30
11JAN2009 1221 30

 

 

as you can see from the program below from 30dec2008 until 31jan2009 amt is 20

yabwon
Onyx | Level 15

Hi,

 

Right, my bad, sorry! 

 

Bart

 

try this one:

data have;
format eff_dat date9. ;
infile cards dlm = ',' ;
input eff_dat date9. id amt;
cards;
30dec2008,1221,20
01feb2009,1221,30
01mar2009,1222,10
01apr2009,1222,15
;
run;


data want;
  loop =  eff_dat; drop loop;
  put _all_;

  set have(keep=eff_dat);

  if loop ne . then
    do _N_ = loop to eff_dat-1;
      eff_dat = _N_;
      output;
    end;

  set have;

run;

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



MV04
Obsidian | Level 7

HI Yabwon ,

That's awsome but I am missing the last record so for exampl if I have got only these three

30dec2008,1221,20
01feb2009,1221,30
01mar2009,1222,10

 

then I am not geting 01mar2009,1222,10 in my result

 

yabwon
Onyx | Level 15

ok, I think that one will fix the issue(I narrowed data in the example, to make it easier to test)

Bart

 

data have;
format eff_dat date9. ;
infile cards dlm = ',' ;
input eff_dat date9. id amt;
cards;
30dec2008,1221,20
05jan2009,1222,30
10jan2009,1223,10
15jan2009,1224,15
;
run;


data want;
  loop =  eff_dat; drop loop;
  put _all_;

  set have(keep=eff_dat);

  if loop ne . then
    do _N_ = loop to eff_dat-1;
      eff_dat = _N_;
      output;
    end;

  set have end=eof;

  if eof then output; 

run;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



MV04
Obsidian | Level 7

Thanks so much Yabwon .

That did work.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 13 replies
  • 996 views
  • 0 likes
  • 4 in conversation