BookmarkSubscribeRSS Feed
RandyStan
Fluorite | Level 6

Dear All:

  I want to create 10 groups for the variable VARA using the 0/1 flag.  So my data is as follows

 

VarA       Flag

10           0

8             0

11           1

24          1

35          0

 

and so forth

 

I want to group the data by VARA and the flag.

 

So I want 

 

VarA       Flag   Group_FLAG_0           Group_FLAG_1

10           0             

8             0            

11           1                                               

24          1

35          0           

 

Can this be done in one step.

Thanks in advance

 

Randy

 

 

5 REPLIES 5
andreas_lds
Jade | Level 19

Maybe it can be done in one step, but since you have not revealed the logic to be applied in an (for me) understandable way, i can't give you any details on how to do it. And, as  side-note, you should start posting data in usable form, so that there is one thing less we need to guess.

ballardw
Super User

What values would be assigned to Group_FLAG_0 and Group_FLAG_1?

What are the rules for assigning the values to the variables?

Krueger
Pyrite | Level 9

How do you want them grouped? What's the logic?

 

data have;
	input VarA Flag;
	datalines;
10 0
8  0
11 1
24 1
35 0
	;
run;
proc sort data = have; by VarA; run;
data want;
	set have;
	by varA;
	if Flag = 0 then Group_FLAG_0 = 1;
	else if Flag = 1 then Group_FLAG_1 = 1; 
run;

 

RandyStan
Fluorite | Level 6

If I may, let me try to explain

I have varA with numbers and a Flag (0,1)

I want to construct a decile column (which gives the decile for VarA when Flag = 0; and when Flag = 1

 

So for example; (A shortened version of the data set)

 

VarA       Flag

10           0

12           0

 3            1

 15          1

25           0

 

And I construct a group_Rank variable such that

 

VarA       Flag      GroupRank_VARA

10           0                1

12           0                2

 15           1               2

 3             1               1

25           0                3

 

 

proc rank data=have out= want groups=10;

by Flag;

var VarA;

rank GroupRank_VarA;

run;

I hope I am clearer this time.

Randy

 

 

ballardw
Super User

I think if you display your want in a clearer form:

VarA       Flag      GroupRank_VARA
10           0                1
12           0                2
25           0                3
 3           1                1
15           1                2

You want:

proc rank data=have out= want groups=10; 
   by Flag; 
   var VarA; 
   ranks GroupRank_VarA; 

run;

Note the S in RANKS.

Your code would have been throwing errors that maybe you should start with.

And as with all BY statements you have to sort the data by the variables on the BY statement before use.

Note that Proc Rank will create ranks of 0 to 9 when requesting groups=10. So if you really must have 1 to 10 you will need to take a pass through a data step to add 1.

From the documentation for Proc Rank

 

GROUPS=number-of-groups

assigns group values ranging from 0 to number-of-groups minus 1.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 5 replies
  • 914 views
  • 0 likes
  • 4 in conversation