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

Rookie here. I am trying to extend my dataset. It has the following structure

 

DateABC
31-Mar-170.20.11.1
30-Jun-170.40.31.3
30-Sep-170.60.71.9

 

I am trying to extend this dataset for a number of periods. Also the variables once the dates are extended need to be a difference between current and previous value. It should follow the following structure:

 

DateABC
31-Mar-170.20.11.1
30-Jun-170.40.31.3
30-Sep-170.60.71.9
31-Dec-170.20.40.6
31-Mar-18-0.4-0.3-1.3
30-Jun-18-0.6-0.7-1.9

 

 

Any ideas?

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

data have;
infile cards expandtabs;
input Date : date11. A	B	C;
format date date11.;
cards;
31-Mar-17	0.2	0.1	1.1
30-Jun-17	0.4	0.3	1.3
30-Sep-17	0.6	0.7	1.9
;
run;

data want;
 set have end=last;
 laga=lag(a);lagb=lag(b);lagc=lag(c);
 output;
 if last then do;
   do i=1 to 3;
    date=intnx('month',date,1,'e');
	_a=a-laga;_b=b-lagb;_c=c-lagc;
	laga=a;lagb=b;lagc=c;
	a=_a;b=_b;c=_c;
	output;
   end;
 end;
 drop i lag: _: ;
 run;

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Post test data in the form of a datastep!

 

 

As such this is only pseudocode:

 

/* This adds a year to the data into a new dataset */
data have1;
  set have;
  date=intnx('year',date,1);
run;

/* set the two datasets */
data want;
  set have (in=a) have1 (in=b);
  if a then do;
    new_a=a;
    new_b=b;
    new_c=c;
  end;
  else do;
    new_a=lag(a)-lag2(a);
    new_b=lag(b)-lag2(b);
    new_c=lag(c)-lag2(c);
  end;
run;

eemrun
Obsidian | Level 7
Apologies. I will make sure to put it in a data step next time.
Ksharp
Super User

data have;
infile cards expandtabs;
input Date : date11. A	B	C;
format date date11.;
cards;
31-Mar-17	0.2	0.1	1.1
30-Jun-17	0.4	0.3	1.3
30-Sep-17	0.6	0.7	1.9
;
run;

data want;
 set have end=last;
 laga=lag(a);lagb=lag(b);lagc=lag(c);
 output;
 if last then do;
   do i=1 to 3;
    date=intnx('month',date,1,'e');
	_a=a-laga;_b=b-lagb;_c=c-lagc;
	laga=a;lagb=b;lagc=c;
	a=_a;b=_b;c=_c;
	output;
   end;
 end;
 drop i lag: _: ;
 run;

eemrun
Obsidian | Level 7
Worked perfectly. Thanks!!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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