Any SAS programmers...
 

Subscribe now and choose from over 30 free gifts worth up to £49 - Plus get £25 to spend in our shop

[Closed] Any SAS programmers about?

10 Posts
6 Users
0 Reactions
76 Views
Posts: 19
Free Member
Topic starter
 

If so can I ask for a little help please. I have bitten off more than I can chew...SAS programming (well all programming) is a little alien to me, I have got my head round the SQL part of it but I don't think SQL is appropriate for the next bit. What I want to do isn't difficult, I have managed it in VBA, unfortunately I have no idea where to start in SAS. Any tips or pointers greatly appreciated.

I have a list of dates for a particular groups, so A could have 4 dates, B 10, C 1 etc. I want to look at a date, compare it to the record (row) previous and if the year is not consecutive then add in as many years to make it consecutive. eg. 23/06/2001, 04/12/2005 (2 rows) I would want to become 23/06/2001, 01/01/2002, 01/01/2003, 01/01/2004, 04/12/2005 (the program inserts 3 new rows). This would be done for each group only, so comparisons between last row of A and first of B are not made.

Apologies for asking here first, I could try a programmers forum but they are scarier than this place!


 
Posted : 22/06/2012 9:53 am
Posts: 19
Free Member
Topic starter
 

bump for lunch...


 
Posted : 22/06/2012 11:35 am
Posts: 0
Free Member
 

It's a long time since I did SAS - before they started using SQL actually.


 
Posted : 22/06/2012 11:37 am
Posts: 10953
Free Member
 

[img] [/img]


 
Posted : 22/06/2012 12:15 pm
Posts: 19
Free Member
Topic starter
 

....Rodney


 
Posted : 22/06/2012 12:19 pm
Posts: 0
Full Member
 

geek 😛


 
Posted : 22/06/2012 12:23 pm
Posts: 932
Free Member
 

What you need to do is sort the dates first into the order you want, i.e. by group. Get the year part of the date and create a 'Do' loop from the start year until you reach the next year, maybe something along the lines of

if start+1 < end then output new row


 
Posted : 22/06/2012 12:24 pm
Posts: 19
Free Member
Topic starter
 

I'm trying...not clever enough though

EDIT - that was to Bigjim. Thanks Alan


 
Posted : 22/06/2012 12:24 pm
 robh
Posts: 0
Full Member
 

You could sort and use a lag function,
Data date;
set orgdata;
if group =lag(group) then do;
if year(date) ne year(lag(date) then do;
date = '01JAN'||year(lag(date));
end;
end;
else output;
run;

or something like that depending on how the data set is structured or sorted.


 
Posted : 22/06/2012 12:29 pm
Posts: 19
Free Member
Topic starter
 

brilliant, thank you. That should set me off on the right track.


 
Posted : 22/06/2012 12:33 pm
 robh
Posts: 0
Full Member
 

Saying that lags and if's together not a good idea.

data dataset2;
format date date9.;
set dataset1;
by group date;

retain prevdate;
drop prevdate i;

if first.group then do;
prevdate = .;
output;
end;
else do;
output;
do i = year(prevdate) + 1 to year(date) - 1;
date = mdy(1, 1, i);
output;
end;
date = mdy(1, 1, i);
end;
prevdate = date;
run;


 
Posted : 22/06/2012 1:11 pm