DATA Step, Macro, Functions and more

How to assign a number to each distinct value for one variable?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 75
Accepted Solution

How to assign a number to each distinct value for one variable?

Hi all,

I have a data set like this one:

   var1   var 2         number

     aa     x                 1

     aa     y                 1 

     aa     z                 1

     bb     t                  2

     bb     y                 2

     cc                        3

     cc                        3

     cc                        3

so, the column number is the column that I want to obtain. As you can see, it assigns a number for each of distinct values of var1.

could you please help me????

thanks a lot


Accepted Solutions
Solution
‎01-29-2013 02:50 PM
Super Contributor
Posts: 543

Re: How to assign a number to each distinct value for one variable?

Hi.

How about this?

data in;

input var1 $ var2 $;

cards;

aa x

aa y

aa z

bb t

bb y

cc .

cc .

cc .

;

proc sort data = in;by var1;

data temp; 

    set in;

    by var1;

    retain number 0;

    if first.var1 then number++ 1;retain number;

run;

I hope it helps!

Good luck.

Anca.

View solution in original post


All Replies
Solution
‎01-29-2013 02:50 PM
Super Contributor
Posts: 543

Re: How to assign a number to each distinct value for one variable?

Hi.

How about this?

data in;

input var1 $ var2 $;

cards;

aa x

aa y

aa z

bb t

bb y

cc .

cc .

cc .

;

proc sort data = in;by var1;

data temp; 

    set in;

    by var1;

    retain number 0;

    if first.var1 then number++ 1;retain number;

run;

I hope it helps!

Good luck.

Anca.

Frequent Contributor
Posts: 75

Re: How to assign a number to each distinct value for one variable?

Thanks a lot, Anca. Thats perfect. I was not familiar with retain Smiley Happy.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 229 views
  • 1 like
  • 2 in conversation