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

I want to regroup the data, can anyone help me?

 

Orginal: (PartNumber and Line Number are sorted)

 

PartNumber    LineNumber   

A                        01

A                        02

A                        03

A                        01

A                        02

A                        01

A                        02

A                        03

A                        04

A                        05

B                        01

B                        01

B                        02

B                        01

 

Target:

 

PartNumber    LineNumber   Group

A                        01                 01

A                        02                 01

A                        03                 01

A                        01                 02

A                        02                 02

A                        01                 03

A                        02                 03

A                        03                 03

A                        04                 03

A                        05                 03

B                        01                 01

B                        01                 02

B                        02                 02

B                        01                 03

 

I want every time when Line number = 1 , the group number +1, every time the part number change , the group number back to 1

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
soham_sas
Quartz | Level 8
 
data have;
infile cards dlm='09'x;
input PartNumber$ LineNumber;
cards;
A	01
A	02
A	03
A	01
A	02
A	01
A	02
A	03
A	04
A	05
B	01
B	01
B	02
B	01
;
run;

data want;
set have;
by PartNumber;
retain group;
if first.PartNumber and linenumber=1 then group=1;
else if linenumber=1 then group+1;
output;
if last.partnumber;
run;

View solution in original post

3 REPLIES 3
soham_sas
Quartz | Level 8
 
data have;
infile cards dlm='09'x;
input PartNumber$ LineNumber;
cards;
A	01
A	02
A	03
A	01
A	02
A	01
A	02
A	03
A	04
A	05
B	01
B	01
B	02
B	01
;
run;

data want;
set have;
by PartNumber;
retain group;
if first.PartNumber and linenumber=1 then group=1;
else if linenumber=1 then group+1;
output;
if last.partnumber;
run;

zhaoxuan210
Fluorite | Level 6
Thank you! Why we still need if last.partnumber in the end?
novinosrin
Tourmaline | Level 20
 
data have;
infile cards ;
input PartNumber$ LineNumber ;
cards;
A	01
A	02
A	03
A	01
A	02
A	01
A	02
A	03
A	04
A	05
B	01
B	01
B	02
B	01
;
run;

data want;
set have;
by partnumber ;
retain group;
if first.partnumber then group=linenumber;
else if linenumber=1 then group+1;
run;

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
  • 3 replies
  • 995 views
  • 1 like
  • 3 in conversation