DATA Step, Macro, Functions and more

Many Variable Names convert from Character to Numeric

Reply
Occasional Contributor
Posts: 18

Many Variable Names convert from Character to Numeric

Good Morning All,


I have a large data set that is reading in an excel file that has character and numeric values in the file.  The variable names are Var5-Var205 and are charater.  I was hoping to write an array-do loop to convert these variables to numeric.  I can't seem to figure it out.  I've looked for another discussion and can't find anything.  If someone knows the solution or has another discussion available, I'd appreciate it.


Have a great day.

PROC Star
Posts: 7,363

Many Variable Names convert from Character to Numeric

How about:

data have;

  retain var5-var205 (201*"12");

  output;

run;

data want (drop=i inSmiley Happy;

  set have (rename=(var5-var205=invar5-invar205));

  array in(201) $ invar:;

  array var(201);

  do i=1 to 201;

    var(i)=input(in(i),best12.);

  end;

run;

Of course, if you don't need your variables to be labeled "var", you would get rid of the annoying notes in the output.

Occasional Contributor
Posts: 18

Many Variable Names convert from Character to Numeric

Art,

I tried doing something similar.  Here is what I wrote:

data final;

set test3;

format var305-var505 13.2;

array variable(201) var5-var205;

array new(201) var305-var505;

          do i =1 to 201;

new(i)=put(variable(i),13.2);

end;

run;

In multiple rows, I have data like 198,487.33 and it outputs it in the new variable as a period.  Same thing happens with code like yours.  Thoughts?

PROC Star
Posts: 7,363

Re: Many Variable Names convert from Character to Numeric

Can you attach a file that contains a couple of records from your original file?  Your code wouldn't work because you: (1) don't indicate that the first array is going to contain character variables and (2) the correct function to use to convert character to numeric would be input, not put.

I would try it again, but using the code i suggested, but use a comma. informat rather than the best12. or whichever one I had suggested.  See: http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000204404.htm

Occasional Contributor
Posts: 18

Many Variable Names convert from Character to Numeric

Using the Comma. works.  Thanks a lot Art!

Ask a Question
Discussion stats
  • 4 replies
  • 244 views
  • 0 likes
  • 2 in conversation