Help using Base SAS procedures

convert to numeric by id?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

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: 7,360

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;

View solution in original post


All Replies
PROC Star
Posts: 7,360

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: 7,360

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?

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

Super User
Posts: 17,784

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.

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

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