BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

Hello

I want to ask how can I create a conditional statement that convert specific variable from char to numeric.

As you understand in some cases the variable will be numeric and then no need to transfer.

And in other cases  variable will be char and then need to transfer it to numeric.

Cases1:

Data a;

input x $;

cards;

1

2

3

;

run;

case2:

Cases1:

Data a;

input x ;

cards;

1

2

3

;

run;

 

 

 

6 REPLIES 6
Jagadishkatam
Amethyst | Level 16
Data a;
input x ;
cards;
1
2
3
;
run;

data b;
length x $10.;
set a(rename=(x=_x));
x=put(_x,best.);
run;
Thanks,
Jag
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Am sure a basic question like this you asked before?

data want;
  set a;
  if notdigit(x) then y=.;
  else y=input(x,best.);
run;
s_lassen
Meteorite | Level 14

The fast and dirty way to do it is this, no need for any conditional statements:

data b;
  length x 8; /* declare X as numeric */
  set a(rename=(x=_x));
  x=_x;
  drop _x;
run;

If the original X is character, you will get a message about conversion in the log.

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Not sure what level you consider "you will get a message about conversion in the log" - this would come out as an issue for my logs.  Implicit conversion should be avoid.

s_lassen
Meteorite | Level 14
Yes, as I said, it is the fast and dirty way to do it.
ballardw
Super User

@Ronein wrote:

Hello

I want to ask how can I create a conditional statement that convert specific variable from char to numeric.

As you understand in some cases the variable will be numeric and then no need to transfer.

And in other cases  variable will be char and then need to transfer it to numeric.

Cases1:

Data a;

input x $;

cards;

1

2

3

;

run;

case2:

Cases1:

Data a;

input x ;

cards;

1

2

3

;

run;

 

 

 


Short answer: you can't. A variable type is set at creation, either numeri or character. If a value is going to hold non-missing character values the variable type must be character. You can have two variables, on character one numeric and conditionally assign VALUES, but not types.

 

If you are attempting to fix data that was imported in an undesired variable type, or inconsistent, then fix the import/ read step to use the correct type consistently. Your "example" is very common when relying on the guessing procedure Proc Import to read data and then combine data sets and discovering that because of the contents of the files Proc Import guessed differently for some files.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1836 views
  • 0 likes
  • 5 in conversation