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

Hi I want to assign a number in ascending order to a date column.

for example I have one column date as

 

date
09/13/2015
09/20/2015
09/27/2015
########
########
10/18/2015
10/25/2015
########
########
11/15/2015
11/22/2015
11/29/2015
########
12/13/2015
12/20/2015
12/27/2015
########
########
01/17/2016
01/24/2016
01/31/2016
########
02/14/2016
02/21/2016
02/28/2016
########
03/13/2016
03/20/2016
03/27/2016
########
########
04/17/2016
04/24/2016
########
########
05/15/2016
05/22/2016
05/29/2016
########
########
06/19/2016
06/26/2016
########
########
07/17/2016
07/24/2016
07/31/2016
########
08/14/2016
08/21/2016
08/28/2016
########

 and I want to assign a number to date (starts from 1 ) like below;

 

dateNumber
09/13/20151
09/20/20152
09/27/20153
########4
########5
10/18/20156
10/25/20157
########8
########9
11/15/201510
11/22/201511
11/29/201512
########13
12/13/201514
12/20/201515
12/27/201516
########17
########18
01/17/201619
01/24/201620
01/31/201621
########22
02/14/201623
02/21/201624
02/28/201625
########26
03/13/201627
03/20/201628
03/27/201629
########30
########31
04/17/201632
04/24/201633
########34
########35
05/15/201636
05/22/201637
05/29/201638
########39
########40
06/19/201641
06/26/201642
########43
########44
07/17/201645
07/24/201646
07/31/201647
########48
08/14/201649
08/21/201650
08/28/201651
########52
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

That's more work than it's worth. You can just sort and use the approach I suggested earlier:

 

proc sort data=have;
by date;
run;

data want;
set have;
by date;
retain counter 0;
if first.date then counter+1;
run;

@ashish112 wrote:

An item does not have same date more than one time, and yes say a date Jan 1 2008 will have only one number across all items. simple way if we remove duplicate dates than only unique date will remains and we can assign a number counter by sorting date.


 

View solution in original post

22 REPLIES 22
novinosrin
Tourmaline | Level 20

data want;

set have;

number=_n_;

run;

 

or

 

data want;

set have;

number+1;

run;

ashish112
Fluorite | Level 6
Thanks for quick reply, let me t iell you my issue in detail. I have a date column where date is not in order, so in the first occurrence of date I want 1, in second occurrence I want 2, there may be cases when the dates are same, so for that dates I want same number. Hope it is clear
novinosrin
Tourmaline | Level 20

Hi @ashish112  Can you post a better sample of what you have and want. Make the sample in excel and paste here. 

ashish112
Fluorite | Level 6

Hi , thanks for reply, I have attached sample file here. I want Period column as in file. For same date the period number also be same. so suppose I have 104 unique date the max number is 104. I hope it is clear now.

Reeza
Super User
proc sort data=have;
by date;
run;

data want;
set have;
by date;
retain counter 0;
if first.date then counter+1;
run;
novinosrin
Tourmaline | Level 20

Ok it seems just a sequence for each item and nothing to do with date

 

data want;

set have;

by item;

if first.item then period=1;

else period+1;

run;

 

Reeza
Super User

@ashish112 wrote:
Thanks for quick reply, let me t iell you my issue in detail. I have a date column where date is not in order, so in the first occurrence of date I want 1, in second occurrence I want 2, there may be cases when the dates are same, so for that dates I want same number. Hope it is clear

You have multiple items AND multiple dates. Can an item have the same date more than once? Are you saying a date, say Jan1 2018 needs to be the same across the different items, or it's just a counter of dates for reach Item?

ashish112
Fluorite | Level 6

An item does not have same date more than one time, and yes say a date Jan 1 2008 will have only one number across all items. simple way if we remove duplicate dates than only unique date will remains and we can assign a number counter by sorting date.

Reeza
Super User

That's more work than it's worth. You can just sort and use the approach I suggested earlier:

 

proc sort data=have;
by date;
run;

data want;
set have;
by date;
retain counter 0;
if first.date then counter+1;
run;

@ashish112 wrote:

An item does not have same date more than one time, and yes say a date Jan 1 2008 will have only one number across all items. simple way if we remove duplicate dates than only unique date will remains and we can assign a number counter by sorting date.


 

ashish112
Fluorite | Level 6
Yes and after that I have to sort again if I want it by Item and date.
Is my code correct?

Data want;
Set have;
By item date;
Run;

Sorry I am learning sas so it is not easy for me right now.
Reeza
Super User

@ashish112 wrote:
Yes and after that I have to sort again if I want it by Item and date.
Is my code correct?

Data want;
Set have;
By item date;
Run;

Sorry I am learning sas so it is not easy for me right now.

Correct for what, you didn't say what you're actually trying to do in that step. If it's sort, then no, it is not correct. You use PROC SORT to sort, similar to what I did in the answer above. Change the BY statement to have it sorted the way you want.

ashish112
Fluorite | Level 6
I first sort the data by date and create variable counter, but now I want
the file in format by item and then by date. So for that I have written
that code.
Reeza
Super User
You sort with PROC SORT or PROC SQL.
ashish112
Fluorite | Level 6
Ok, got it,sometimes its confusing for the beginner. Thanks for your help.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 22 replies
  • 2754 views
  • 4 likes
  • 8 in conversation