BookmarkSubscribeRSS Feed
kpdoe
Calcite | Level 5

Hello, I have an excel file which I create a dataset from and want to assign the group #. How can I achieve the below scenario?

 

Have:

IDQTYGROUP
1  
20.052
30.052
4  
50.055
60.055
7  
8  

 

Want:

IDQTYGROUP
1 1
20.052
30.052
4 3
50.055
60.055
7 6
8 7
8 REPLIES 8
andreas_lds
Jade | Level 19

Please post data in usable form, so that we don't have to guess types etc. Have a look at https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... if you need guidance.

ed_sas_member
Meteorite | Level 14

Hi @kpdoe 

 

Here is some code you can try.

Please check carefully the rules to assign the group number :

- if the group already exists : should be the existing value

- if the group is missing : should be the previous value + 1

- first row : should be 1

Is that correct ?

 

Best,

 

data have;
	infile datalines truncover;
	input ID QTY GROUP;
	datalines;
1
2 0.05 2
3 0.05 2
4
5 0.05 5
6 0.05 5
7
8
;
run;

data want;
	set have;
	if nmiss(QTY) = 0 			  then _group = GROUP;
	if nmiss(QTY) = 1 			  then _group + 1;
	if _n_ = 1 and nmiss(QTY) = 1 then _group = 1;
	drop GROUP;
	rename _group = GROUP;
run;

 

kpdoe
Calcite | Level 5
I tried this method but the all the missing group is assigned as 1
Ksharp
Super User
data have;
	infile datalines truncover;
	input ID QTY GROUP;
	datalines;
1
2 0.05 2
3 0.05 2
4
5 0.05 5
6 0.05 5
7
8
;
data want;
 set have;
 retain new_group ;
 if not missing(group) then new_group=group;
  else new_group+1;
run;
Astounding
PROC Star

Was skipping from 3 to 5 intentional, or an accident?

 

What should happen if QTY and/or GROUP changes from one non-missing value to another?

 

ID QTY GROUP
1    
2 0.05 2
3 0.77 4
4    
5 0.05 5
6 0.05 5
7    
8    
kpdoe
Calcite | Level 5
Yes it was intentional. I need to do sampling
ballardw
Super User

@kpdoe wrote:
Yes it was intentional. I need to do sampling

Then perhaps you should describe the sampling rules instead of an intermediate step.

kpdoe
Calcite | Level 5
 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1535 views
  • 0 likes
  • 6 in conversation