## convert to numeric by id?

Solved
Occasional Contributor
Posts: 10

# convert to numeric by id?

need to convert id from character to numeric in the following fashion (to use in a panel data regression later):

idname  idnumber

a       1

a       1

a       1

a       1

a       1

b       2

b       2

b       2

c       3
c       3
c       3
c       3

Accepted Solutions
Solution
‎08-22-2011 05:16 PM
PROC Star
Posts: 8,165

## Re: convert to numeric by id?

Then, sort your file by idname if it isn't already sorted and then run:

data want;

set have;

by idname;

if first.idname then idnumber+1;

run;

All Replies
PROC Star
Posts: 8,165

## convert to numeric by id?

I'm not sure if your example was exactly representing what you want but, if it is, you could use:

data want;

set have;

idnumber=rank(idname)-96;

run;

Occasional Contributor
Posts: 10

## Re: convert to numeric by id?

hmm.. not exactly.  i tried this, idnumber=rank(idnname)-50;

it starts with 1 but then misses some numbers. it shows as 1, 2, 4, 15.. and ends with 40 although i have 300 stocks (so the last idnumber should be 300).

there are just stock names, so should be by 'idname'. ( PROC PANEL  will only run if for each stock  a numeric  is assigned)

idname   year idnumber

stockA        96               1

stockA        97               1

stockA        98               1

stockB        96               2
stockB        97               2

stockC        96               3

stockC        97               3

stockC        98               3

stockC        99               3

Solution
‎08-22-2011 05:16 PM
PROC Star
Posts: 8,165

## Re: convert to numeric by id?

Then, sort your file by idname if it isn't already sorted and then run:

data want;

set have;

by idname;

if first.idname then idnumber+1;

run;

Occasional Contributor
Posts: 10

## Re: convert to numeric by id?

yep. told it'd be simple.  have tried a similar command before myself but somehow did not work.

Super User
Posts: 23,771

## Re: convert to numeric by id?

I'm curious as to why you can't use StockA and need a number to identify each?

Usually you just add the idname in the class statement..

Occasional Contributor
Posts: 10

## Re: convert to numeric by id?

bcos i need to run PROG PANEL later, as i said, and it needs string or numeric.

🔒 This topic is solved and locked.

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

Discussion stats
• 6 replies
• 205 views
• 0 likes
• 3 in conversation