BookmarkSubscribeRSS Feed
mkeintz
PROC Star

I'm late coming into this discussion, but it appears to me that you want the last observed date in each month, which I interpret differently than the ordinary last date.

 

If so, then you can run a program that looks ahead one record to see if the next record is in a different month than the current record:

 

proc sort data=have out=need;
  by date;
run;

data want (drop=nxt_date);
  merge need
        need (firstobs=2 keep=date rename=(date=nxt_date));
  if intck('month',date,nxt_date);
run;

If your original dataset (named "have" here) is already sorted, skip the proc sort, and apply the merge statement to have instead of need.

 

The merge statement combines each observation with a single, renamed variable (date=nxt_date)  from the following observation (that's from the "firstobs=2" parameter).  This allows the intck function to count the number of months between date and nxt_date.  Only non-zero results pass the filter - yielding the last observed date of the month.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
SASlearner97
Obsidian | Level 7
Thank you so much

in my var column i have 4 kinds of observations like :

var
a1
a2
a3
a4
....

when i tried your code it went like :
var date
a1 31/01/2013
a1
a1
a1
...
the dates are correct but the column var is wrong ..
mkeintz
PROC Star

@SASlearner97 wrote:
Thank you so much

in my var column i have 4 kinds of observations like :

var
a1
a2
a3
a4
....

when i tried your code it went like :
var date
a1 31/01/2013
a1
a1
a1
...
the dates are correct but the column var is wrong ..

Then I don't understand what you want, and probably I don't understand what you are starting with.  Please provide, in the form of a SAS data step, a sample of the data you are starting with (call it data set HAVE), and a sample of what it should provide, also in the from of a data step.  Then I can provide code to work with what you have, as opposed to guessing what you have. 

 

Help us help you.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 17 replies
  • 1243 views
  • 3 likes
  • 6 in conversation