BookmarkSubscribeRSS Feed
gabras
Pyrite | Level 9

Hi everybody,

 

i need to enumerate the records based on three variables. The dataset must be orderd in ascending order by date.

 

I tried the following,

 

data want;
set have;
ord + 1;
by ID CREATED PROD SUBPROD  ;
if first.ID or first.PROD or first.SUBPROD
then ord = 1;

run;

 

but it doesn't work as you can se in the table below:

 

IDCREATEDPRODSUBPRODORD
101-ott-18AA1
102-ott-18AA1
102-ott-18AA2
102-ott-18AA3
105-ott-18BB1
202-ott-18AA1
303-ott-18BB1
304-ott-18BB1
305-ott-18BB1
401-ott-18CC1
402-ott-18CC1
402-ott-18CC2
402-ott-18DD1
405-ott-18DD1
406-ott-18DD1

 

The data set i want has to be like this:

 

IDCREATEDPRODSUBPRODORD
101-ott-18AA1
102-ott-18AA2
102-ott-18AA3
102-ott-18AA4
105-ott-18BB1
202-ott-18AA1
303-ott-18BB1
304-ott-18BB2
305-ott-18BB3
401-ott-18CC1
402-ott-18CC2
402-ott-18CC3
402-ott-18DD1
405-ott-18DD2
406-ott-18DD3

 

Thank you

13 REPLIES 13
PaigeMiller
Diamond | Level 26

Without considering date:

 

proc sort data=have;
    by id prod subprod;
run;

data want;
    set have;
    by id prod subprod;
    if first.subprod then ord=0;
    ord+1;
run;
--
Paige Miller
gabras
Pyrite | Level 9
Is the table have sorted in ascending order by date?
PaigeMiller
Diamond | Level 26

So you can add date into the PROC SORT at the end of the BY statement.

--
Paige Miller
gabras
Pyrite | Level 9

 

Ok, i'll try .

 I have to ask yo one question.

Why you wrote just first.subprod in the if statement without writing the three variables?

 

Thank you

novinosrin
Tourmaline | Level 20

If your data is already sorted by date, 

 

data have;
input (ID CREATED PROD SUBPROD) ($);
cards;
1 01-ott-18 A A 1
1 02-ott-18 A A 1
1 02-ott-18 A A 2
1 02-ott-18 A A 3
1 05-ott-18 B B 1
2 02-ott-18 A A 1
3 03-ott-18 B B 1
3 04-ott-18 B B 1
3 05-ott-18 B B 1
4 01-ott-18 C C 1
4 02-ott-18 C C 1
4 02-ott-18 C C 2
4 02-ott-18 D D 1
4 05-ott-18 D D 1
4 06-ott-18 D D 1
;

data want;
set have;
by ID PROD SUBPROD ;
if first.subprod then ord=1;
else ord+1;
run;

mkeintz
PROC Star

Just to be cute, here is a one-step solution, possibly handy with a large (but sorted) raw data file:

 

data have;
input (ID CREATED PROD SUBPROD) ($); 
  ord+1;
  if     catx(' ',id,prod,subprod)
   ^=lag(catx(' ',id,prod,subprod)) then ord=1;
cards;
1 01-ott-18 A A 1
1 02-ott-18 A A 1
1 02-ott-18 A A 2
1 02-ott-18 A A 3
1 05-ott-18 B B 1
2 02-ott-18 A A 1
3 03-ott-18 B B 1
3 04-ott-18 B B 1
3 05-ott-18 B B 1
4 01-ott-18 C C 1
4 02-ott-18 C C 1
4 02-ott-18 C C 2
4 02-ott-18 D D 1
4 05-ott-18 D D 1
4 06-ott-18 D D 1
;
--------------------------
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

--------------------------
PaigeMiller
Diamond | Level 26

@gabras wrote:

 

Ok, i'll try .

 I have to ask yo one question.

Why you wrote just first.subprod in the if statement without writing the three variables?

 

Thank you


It is not necessary (and wrong) to include all three variables. You want the ORD variable to reset when SUBPROD changed, not when all three change.

--
Paige Miller
gabras
Pyrite | Level 9

I was too superficial.

I can also have the following situation:

IDCREATEDPRODSUBPRODITEM
101-ott-18ABD
102-ott-18ABD
102-ott-18ABD
102-ott-18ABD
105-ott-18AAE


and i want this 

 

IDCREATEDPRODSUBPRODITEMORD
101-ott-18ABD1
102-ott-18ABD2
102-ott-18ABD3
102-ott-18ABD4
105-ott-18AAE1
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

did you try @mkeintz code.  That logic should work in your case.

 

data have;
input (ID CREATED PROD SUBPROD) ($); 
  ord+1;
  if     catx(' ',id,prod,subprod)
   ^=lag(catx(' ',id,prod,subprod)) then ord=1;
cards;
1 01-ott-18 A A 1
1 02-ott-18 A A 1
1 02-ott-18 A A 2
1 02-ott-18 A A 3
1 05-ott-18 B B 1
2 02-ott-18 A A 1
3 03-ott-18 B B 1
3 04-ott-18 B B 1
3 05-ott-18 B B 1
4 01-ott-18 C C 1
4 02-ott-18 C C 1
4 02-ott-18 C C 2
4 02-ott-18 D D 1
4 05-ott-18 D D 1
4 06-ott-18 D D 1
;

 

PaigeMiller
Diamond | Level 26

@gabras wrote:

I was too superficial.

I can also have the following situation:

ID CREATED PROD SUBPROD ITEM
1 01-ott-18 A B D
1 02-ott-18 A B D
1 02-ott-18 A B D
1 02-ott-18 A B D
1 05-ott-18 A A E


and i want this 

 

ID CREATED PROD SUBPROD ITEM ORD
1 01-ott-18 A B D 1
1 02-ott-18 A B D 2
1 02-ott-18 A B D 3
1 02-ott-18 A B D 4
1 05-ott-18 A A E 1

Surely you can modify the code I provided to handle this situation. 

--
Paige Miller
gabras
Pyrite | Level 9
Could you help me please
mkeintz
PROC Star

You have asked for help.  

 

Help was provided.

 

If you need more help, please show the code you tried, and show the result of that code, and describe what part of the result is in error.

 

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

--------------------------
PaigeMiller
Diamond | Level 26

@gabras wrote:
Could you help me please

Earlier, I stated the explanation

 

It is not necessary (and wrong) to include all three variables. You want the ORD variable to reset when SUBPROD changed, not when all three change.

 

so apply this explanation to your new situation involving variable ITEM.

--
Paige Miller

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 13 replies
  • 1491 views
  • 2 likes
  • 5 in conversation