DATA Step, Macro, Functions and more

Is there a better way to change var from char to num?

Reply
Occasional Contributor
Posts: 18

Is there a better way to change var from char to num?

Just wondering if there is a more efficient way to charge a var from char to num.   Often I import dataset and find out in a merge that the varible isn’t the same type.  This is how I do it

 

Data hello;

     Set Bye;

     NumKite = input(kite, 8.);

     Drop kite;

     Rename NumKite = Kite;

run;

 

Is there a better way rather then the drop and rename?

 

thank you for your help

Super User
Posts: 4,031

Re: Is there a better way to change var from char to num?

By far the best approach is to import it into the correct data type to begin with and not convert it at all. I bet you are importing from Excel which has a mind of its own when deciding in what format it will deliver data. One way to enforce correct data types is to import from CSV files and not Excel. Is this a possibility for you?

Occasional Contributor
Posts: 18

Re: Is there a better way to change var from char to num?

Usually the files I get are in excel and just for a one time use. I often have to merge the file with another file to analyze it. I could save it as a .csv but don't want to go to all try trouble of defining all the variables in an infile statement. I like that sas can guess on an excel file but just was thinking there might be a easier way to change the type.

Thanks
Super User
Posts: 4,031

Re: Is there a better way to change var from char to num?

You don't have to define all your variables to import a CSV file. You can use PROC IMPORT with DBMS = CSV similar to Excel. Using the GUESSINGROWS option as well will probably get you a better result than with Excel.

Super User
Super User
Posts: 8,289

Re: Is there a better way to change var from char to num?

Not really.

If you are doing your "merge" using SQL then you can just do the conversion there.

proc sql ;
create table want as
select * 
from a , b
where a.kite = input(b.kite,32.)
;
quit;
Ask a Question
Discussion stats
  • 4 replies
  • 155 views
  • 0 likes
  • 3 in conversation