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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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