DATA Step, Macro, Functions and more

Timeseries data - subsetting

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Timeseries data - subsetting

Hi.

I have a small problem with creating a dataset I need for an analysis.

Lets assume I have an ID variable  "serial", and two different string variables: - "period" in the form 201403

                                                                                                              - "period_renewed" in the same form

First what I want to do is to delete all data where period>period_renewed.

Further I want to keep only the observations from the last 6 periods from the point in time where period=period_renewed.

Someone out there might have a simple solution to this problem, but I'm completely blank as I'm quite new to using SAS.

Any feedback would be extremely welcome. Smiley Happy

/petter


Accepted Solutions
Solution
‎05-20-2014 09:45 PM
Respected Advisor
Posts: 4,925

Re: Timeseries data - subsetting

Assuming there are no duplicates :

proc sort data=have(where=( period<=period_renewed)) out=temp; by serial descending period; run;

data want;

i = 0;

do until (last.serial);

  set temp; by serial;

  i + 1;

  if i <= 6 then output;

  end;

drop i;

run;

proc sort data=want; by serial period; run;

(Untested)

PG

PG

View solution in original post


All Replies
Contributor
Posts: 39

Re: Timeseries data - subsetting

the following will create a dataset called subset that excludes anything where period>period_renewed:

data subset;

     set original;

     if period>period_renewed then delete;

run;

alternatively, you could use:

data subset;

     set original;

     where period<=period_renewed;

run;

not really sure what you are asking for in the second part.

Solution
‎05-20-2014 09:45 PM
Respected Advisor
Posts: 4,925

Re: Timeseries data - subsetting

Assuming there are no duplicates :

proc sort data=have(where=( period<=period_renewed)) out=temp; by serial descending period; run;

data want;

i = 0;

do until (last.serial);

  set temp; by serial;

  i + 1;

  if i <= 6 then output;

  end;

drop i;

run;

proc sort data=want; by serial period; run;

(Untested)

PG

PG
Respected Advisor
Posts: 4,925

Re: Timeseries data - subsetting

Or perhaps, simpler:

proc sort data=have; by serial period; run;

data want;

retain firstSerial;

set temp; by serial;

if first.serial then firstSerial = _n_;

if period>=period_renewed then do;

  do point = max(firstSerial, _n_ - 5) to _n_;

       set temp point=point;

       output;

       end;

  firstSerial = constant('BIG');

  end;

drop point firstSerial;

run;

(Untested)

PG

PG
New Contributor
Posts: 2

Re: Timeseries data - subsetting

Thank you both for your suggestions Smiley Happy 
I solved the problem through changing format of the string to a date variable via the input function,

and then used the intck function + an extra variable to select the right interval.

@PGstats I learned something new from you! Thanks

/Petter

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 341 views
  • 6 likes
  • 3 in conversation