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