How to check if a variable is numeric and convert to a character with a condition?

Accepted Solution Solved
Reply
Contributor
Posts: 25
Accepted Solution

How to check if a variable is numeric and convert to a character with a condition?

Hello Community,

I am just following up with my question in Check if the variable is numeric then convert to Char . 

Thank you all for your valuable replies.

 

In my case I am reading from different XML files, So I dont know if it is numeric or character datatype originally. So ideally what I need to do is something like

%if (%sysfunc(vartype(xyz.myVar)) = N) %then  %do;
put(xyz.myVar,$5.);
%end

proc sql;
  create table abc as 
  select myvar
  from xyz;
  quit;

 

In this way no matter what the data type is , I can always read a character.

 

Please help 


Accepted Solutions
Solution
a week ago
Grand Advisor
Posts: 17,444

Re: How to check if a variable is numeric and convert to a character with a condition?

I think the CAT solution proposed would work, did that not work for your for some reason?

 

option missing='';
data class;
set sashelp.class;
if name in ('Alfred' 'Jane') then age=.;
run;


proc sql;
create table want as
select name, catt(age) as age, weight, height
from class;
quit;

View solution in original post


All Replies
Solution
a week ago
Grand Advisor
Posts: 17,444

Re: How to check if a variable is numeric and convert to a character with a condition?

I think the CAT solution proposed would work, did that not work for your for some reason?

 

option missing='';
data class;
set sashelp.class;
if name in ('Alfred' 'Jane') then age=.;
run;


proc sql;
create table want as
select name, catt(age) as age, weight, height
from class;
quit;

Contributor
Posts: 25

Re: How to check if a variable is numeric and convert to a character with a condition?

Thank you for the quick reply.

Although this one runs and changes the data type ; the value is missing

Grand Advisor
Posts: 17,444

Re: How to check if a variable is numeric and convert to a character with a condition?


jjames1 wrote:

Thank you for the quick reply.

Although this one runs and changes the data type ; the value is missing


I don't understand what that means.

Contributor
Posts: 25

Re: How to check if a variable is numeric and convert to a character with a condition?

Hello Reeza,

It was an error from my side.

This way of conversion worked.

Thank you so much Smiley Happy

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 94 views
  • 1 like
  • 2 in conversation