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

I am trying to add sequence to the following data based in fill_year_month:

 

IDGroupDateyear_month
1BETA BLOCKERS27-Feb-12Feb-12
1ANTIDEPRESSANTS27-Feb-12Feb-12
1ANTIHYPERTENSIVES27-Feb-12Feb-12
1Calcium Channel Blockers27-Feb-12Feb-12
1DIURETICS12-Mar-12Mar-12
1Calcium Channel Blockers29-Mar-12Mar-12
1ANTIDIABETICS29-Mar-12Mar-12
1VITAMINS29-Mar-12Mar-12
1ANTIHYPERTENSIVES29-Mar-12Mar-12
1BETA BLOCKERS29-Mar-12Mar-12
1ANTIDEPRESSANTS29-Mar-12Mar-12
1Anti-infective Agents - Misc.21-Apr-12Apr-12
1ANTIDIABETICS26-Apr-12Apr-12
1DIAGNOSTIC PRODUCTS26-Apr-12Apr-12
1MUSCULOSKELETAL THERAPY AGENTS14-May-12May-12
1DIURETICS14-May-12May-12
1Antifungals17-May-12May-12
1MUSCULOSKELETAL THERAPY AGENTS18-Jun-12Jun-12
1Calcium Channel Blockers23-Jun-12Jun-12
1ANTIHYPERTENSIVES23-Jun-12Jun-12
1ANTIDEPRESSANTS23-Jun-12Jun-12
1BETA BLOCKERS23-Jun-12Jun-12
1DIURETICS23-Jun-12Jun-12
1DIAGNOSTIC PRODUCTS23-Jun-12Jun-12
1MEDICAL DEVICES AND SUPPLIES27-Jul-12Jul-12
1DIAGNOSTIC PRODUCTS27-Jul-12Jul-12
2ANTIHYPERTENSIVES10-Jan-12Jan-12
2ANTICONVULSANTS10-Jan-12Jan-12
2ESTROGENS10-Jan-12Jan-12
2ANTIDEPRESSANTS10-Jan-12Jan-12
2ANTIHYPERLIPIDEMICS10-Jan-12Jan-12
2ULCER DRUGS10-Jan-12Jan-12
2HYPNOTICS/SEDATIVES/SLEEP DISORDER AGENTS10-Jan-12Jan-12
2DIURETICS10-Jan-12Jan-12
2ANALGESICS - OPIOID11-Jan-12Jan-12
2CEPHALOSPORINS11-Jan-12Jan-12
2ANALGESICS - OPIOID11-Jan-12Jan-12
2ULCER DRUGS9-Feb-12Feb-12
2ANTIDEPRESSANTS9-Feb-12Feb-12
2ESTROGENS9-Feb-12Feb-12
2ANTIHYPERTENSIVES9-Feb-12Feb-12
2ANTICONVULSANTS9-Feb-12Feb-12
2ANTIHYPERLIPIDEMICS9-Feb-12Feb-12
2HYPNOTICS/SEDATIVES/SLEEP DISORDER AGENTS10-Feb-12Feb-12
2DIURETICS10-Feb-12Feb-12
2ESTROGENS12-Mar-12Mar-12
2ANTICONVULSANTS12-Mar-12Mar-12
2HYPNOTICS/SEDATIVES/SLEEP DISORDER AGENTS12-Mar-12Mar-12
2DIURETICS12-Mar-12Mar-12
2ANTIHYPERLIPIDEMICS12-Mar-12Mar-12
2ANTIHYPERTENSIVES12-Mar-12Mar-12
2ULCER DRUGS12-Mar-12Mar-12
2ANTIDEPRESSANTS14-Mar-12Mar-12

 

I want the following but if I sort by ID and year_month, the sequence changes. Please advise. 

IDGroupDateyear_monthSeq
1BETA BLOCKERS27-Feb-12Feb-121
1ANTIDEPRESSANTS27-Feb-12Feb-122
1ANTIHYPERTENSIVES27-Feb-12Feb-123
1Calcium Channel Blockers27-Feb-12Feb-124
1DIURETICS12-Mar-12Mar-121
1Calcium Channel Blockers29-Mar-12Mar-122
1ANTIDIABETICS29-Mar-12Mar-123
1VITAMINS29-Mar-12Mar-124
1ANTIHYPERTENSIVES29-Mar-12Mar-125
1BETA BLOCKERS29-Mar-12Mar-126
1ANTIDEPRESSANTS29-Mar-12Mar-127
1Anti-infective Agents - Misc.21-Apr-12Apr-121
1ANTIDIABETICS26-Apr-12Apr-122
1DIAGNOSTIC PRODUCTS26-Apr-12Apr-123
1MUSCULOSKELETAL THERAPY AGENTS14-May-12May-121
1DIURETICS14-May-12May-122
1Antifungals17-May-12May-123
1MUSCULOSKELETAL THERAPY AGENTS18-Jun-12Jun-121
1Calcium Channel Blockers23-Jun-12Jun-122
1ANTIHYPERTENSIVES23-Jun-12Jun-123
1ANTIDEPRESSANTS23-Jun-12Jun-124
1BETA BLOCKERS23-Jun-12Jun-125
1DIURETICS23-Jun-12Jun-126
1DIAGNOSTIC PRODUCTS23-Jun-12Jun-127
1MEDICAL DEVICES AND SUPPLIES27-Jul-12Jul-121
1DIAGNOSTIC PRODUCTS27-Jul-12Jul-122
2ANTIHYPERTENSIVES10-Jan-12Jan-121
2ANTICONVULSANTS10-Jan-12Jan-122
2ESTROGENS10-Jan-12Jan-123
2ANTIDEPRESSANTS10-Jan-12Jan-124
2ANTIHYPERLIPIDEMICS10-Jan-12Jan-125
2ULCER DRUGS10-Jan-12Jan-126
2HYPNOTICS/SEDATIVES/SLEEP DISORDER AGENTS10-Jan-12Jan-127
2DIURETICS10-Jan-12Jan-128
2ANALGESICS - OPIOID11-Jan-12Jan-129
2CEPHALOSPORINS11-Jan-12Jan-1210
2ANALGESICS - OPIOID11-Jan-12Jan-1211
2ULCER DRUGS9-Feb-12Feb-121
2ANTIDEPRESSANTS9-Feb-12Feb-122
2ESTROGENS9-Feb-12Feb-123
2ANTIHYPERTENSIVES9-Feb-12Feb-124
2ANTICONVULSANTS9-Feb-12Feb-125
2ANTIHYPERLIPIDEMICS9-Feb-12Feb-126
2HYPNOTICS/SEDATIVES/SLEEP DISORDER AGENTS10-Feb-12Feb-127
2DIURETICS10-Feb-12Feb-128
2ESTROGENS12-Mar-12Mar-121
2ANTICONVULSANTS12-Mar-12Mar-122
2HYPNOTICS/SEDATIVES/SLEEP DISORDER AGENTS12-Mar-12Mar-123
2DIURETICS12-Mar-12Mar-124
2ANTIHYPERLIPIDEMICS12-Mar-12Mar-125
2ANTIHYPERTENSIVES12-Mar-12Mar-126
2ULCER DRUGS12-Mar-12Mar-127
2ANTIDEPRESSANTS14-Mar-12Mar-128
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

If you want to use BY groups that are grouped but not actually sorted use the NOTSORTED keyword on the BY statement.

proc sort data=have;
  by id date;
run;
data want ;
  set have;
  by id year_month notsorted;
  if first.year_month then seq=0;
  seq+1;
run;

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26
data want;
    set have;
    by id year_month;
    if first.year_month then seq=0;
    seq+1;
run;

This assumes the data is sorted by ID and YEAR_MONTH.

--
Paige Miller
monali
Obsidian | Level 7
Thanks. The problem with this is data get sorted by year_month and sequence
changes. E.g. April2012 gets Seq 1 instead of Feb 2012.
Reeza
Super User
I think you need to explain this issue in more detail. In your example above, each month starts at 1.
ballardw
Super User

@monali wrote:
Thanks. The problem with this is data get sorted by year_month and sequence
changes. E.g. April2012 gets Seq 1 instead of Feb 2012.

If the DATE is an actual SAS date variable sort by that but use the year_month in the by statement of the data step. Unless you have done something very odd to get the year_month variable they should work for the first. and last. syntax.

 

 

Reeza
Super User
Also note the GROUPFORMAT option on the BY statement so that you don't have to create that new variable unless you really need it for the month/date.
Tom
Super User Tom
Super User

If you want to use BY groups that are grouped but not actually sorted use the NOTSORTED keyword on the BY statement.

proc sort data=have;
  by id date;
run;
data want ;
  set have;
  by id year_month notsorted;
  if first.year_month then seq=0;
  seq+1;
run;
Satish_Parida
Lapis Lazuli | Level 10
data have;
infile cards dlm='|';
input ID:$1. Group $ Date:date9. year_month:monyy6.;
format date date9. year_month monyy6.;
cards;
1|BETA BLOCKERS|27-Feb-12|Feb-12
1|ANTIDEPRESSANTS|27-Feb-12|Feb-12
1|ANTIHYPERTENSIVES|27-Feb-12|Feb-12
1|Calcium Channel Blockers|27-Feb-12|Feb-12
1|DIURETICS|12-Mar-12|Mar-12
1|Calcium Channel Blockers|29-Mar-12|Mar-12
1|ANTIDIABETICS|29-Mar-12|Mar-12
1|VITAMINS|29-Mar-12|Mar-12
1|ANTIHYPERTENSIVES|29-Mar-12|Mar-12
1|BETA BLOCKERS|29-Mar-12|Mar-12
1|ANTIDEPRESSANTS|29-Mar-12|Mar-12
1|Anti-infective Agents - Misc.|21-Apr-12|Apr-12
1|ANTIDIABETICS|26-Apr-12|Apr-12
1|DIAGNOSTIC PRODUCTS|26-Apr-12|Apr-12
1|MUSCULOSKELETAL THERAPY AGENTS|14-May-12|May-12
1|DIURETICS|14-May-12|May-12
1|Antifungals|17-May-12|May-12
1|MUSCULOSKELETAL THERAPY AGENTS|18-Jun-12|Jun-12
1|Calcium Channel Blockers|23-Jun-12|Jun-12
1|ANTIHYPERTENSIVES|23-Jun-12|Jun-12
1|ANTIDEPRESSANTS|23-Jun-12|Jun-12
1|BETA BLOCKERS|23-Jun-12|Jun-12
1|DIURETICS|23-Jun-12|Jun-12
1|DIAGNOSTIC PRODUCTS|23-Jun-12|Jun-12
1|MEDICAL DEVICES AND SUPPLIES|27-Jul-12|Jul-12
1|DIAGNOSTIC PRODUCTS|27-Jul-12|Jul-12
2|ANTIHYPERTENSIVES|10-Jan-12|Jan-12
2|ANTICONVULSANTS|10-Jan-12|Jan-12
2|ESTROGENS|10-Jan-12|Jan-12
2|ANTIDEPRESSANTS|10-Jan-12|Jan-12
2|ANTIHYPERLIPIDEMICS|10-Jan-12|Jan-12
2|ULCER DRUGS|10-Jan-12|Jan-12
2|HYPNOTICS/SEDATIVES/SLEEP DISORDER AGENTS|10-Jan-12|Jan-12
2|DIURETICS|10-Jan-12|Jan-12
2|ANALGESICS - OPIOID|11-Jan-12|Jan-12
2|CEPHALOSPORINS|11-Jan-12|Jan-12
2|ANALGESICS - OPIOID|11-Jan-12|Jan-12
2|ULCER DRUGS|9-Feb-12|Feb-12
2|ANTIDEPRESSANTS|9-Feb-12|Feb-12
2|ESTROGENS|9-Feb-12|Feb-12
2|ANTIHYPERTENSIVES|9-Feb-12|Feb-12
2|ANTICONVULSANTS|9-Feb-12|Feb-12
2|ANTIHYPERLIPIDEMICS|9-Feb-12|Feb-12
2|HYPNOTICS/SEDATIVES/SLEEP DISORDER AGENTS|10-Feb-12|Feb-12
2|DIURETICS|10-Feb-12|Feb-12
2|ESTROGENS|12-Mar-12|Mar-12
2|ANTICONVULSANTS|12-Mar-12|Mar-12
2|HYPNOTICS/SEDATIVES/SLEEP DISORDER AGENTS|12-Mar-12|Mar-12
2|DIURETICS|12-Mar-12|Mar-12
2|ANTIHYPERLIPIDEMICS|12-Mar-12|Mar-12
2|ANTIHYPERTENSIVES|12-Mar-12|Mar-12
2|ULCER DRUGS|12-Mar-12|Mar-12
2|ANTIDEPRESSANTS|14-Mar-12|Mar-12
;
run;

proc sort data=have;
by id year_month;
run;

data want;
set have;
by id year_month;
if first.year_month then seq=1;
else seq+1;
run;

Please let us know if it worked for you.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 7 replies
  • 1782 views
  • 1 like
  • 6 in conversation