Help using Base SAS procedures

adding id variable

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 118
Accepted Solution

adding id variable

I have this table

A      B    

10     0.1  

10    0.1    

22     0.1   

22    0.2    

22    0.2    

31     0.2    

31     0.3    

I want to add ID

A      B     ID

10     0.1    1

10    0.1     1

22     0.1    2

22    0.2     2

22    0.2     2

31     0.2     3

31     0.3     3


Accepted Solutions
Solution
‎11-11-2014 10:46 AM
Trusted Advisor
Posts: 1,204

Re: adding id variable

data want;

set have;

by a;

if first.a then id+1;

run;

View solution in original post


All Replies
Solution
‎11-11-2014 10:46 AM
Trusted Advisor
Posts: 1,204

Re: adding id variable

data want;

set have;

by a;

if first.a then id+1;

run;

Frequent Contributor
Posts: 118

Re: adding id variable

I can not do that because A is not properly sorted???????????????

Trusted Advisor
Posts: 1,204

Re: adding id variable

What is the criteria to create id then?

Frequent Contributor
Posts: 118

Re: adding id variable

the criteria is that A must be the same for the id=1.

Super User
Super User
Posts: 7,408

Re: adding id variable

Then sort the data.  If you need original sorting then do:

data have;

     set a;

     prev_sort=_n_;

run;

proc sort data=have;

     by a b;

run;

data want;

set have;

by a;

if first.a then id+1;

run;

proc sort data=want;

     by prev_sort;

run;

However, if your test data is right (and after this sort statement I am not sure), just say:

data want;

     set have;

     do I=1 to 100;

          if (i *10) < a <= ((I+1) * 10) then id=I;

     end;

run;

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 224 views
  • 3 likes
  • 3 in conversation