How To Seprate Character Variable and Numeric Variable

Reply
Contributor
Posts: 32

How To Seprate Character Variable and Numeric Variable

Hi

I Created a Dataset In a Character Variable it contains character and Numeric Value,

I want to Split character and Numeric Value

I Have a dataset which is subject and result are character variable

subjectResult
100120
1002>50
1003Positive
100430.5

I Want the Dataset to be

subjectResultNumericCharacter

1001

2020
1002>50>50
1003PositivePositive
100430.530.5


Could Any one Help Me

Super User
Posts: 5,254

Re: How To Seprate Character Variable and Numeric Variable

The easiest way (I think) is to input the value to the numeric column using best. format. If the result is missing, store the value in the character column.

You'll have some notes in the log about invalid arguments to the input function, but that is expected.

Data never sleeps
Super Contributor
Posts: 644

Re: How To Seprate Character Variable and Numeric Variable

Warning messages can be suppressed using ??

Numeric = input(result, ?? best.) ;

If numeric = . then character = result ;

Richard

Super User
Posts: 5,254

Re: How To Seprate Character Variable and Numeric Variable

Forgot that:smileyblush:.

Works only if you are using the data step, not in SQL.

Data never sleeps
Super Contributor
Posts: 644

Re: How To Seprate Character Variable and Numeric Variable

In my recollection proc SQL the input function does not generate warning messages in this situation, so the ?? is not necessary anyway.  The OP can use SQL to achieve the same result but in this instance a data step is the simplest solution.

Richard

Contributor
Posts: 32

Re: How To Seprate Character Variable and Numeric Variable

Could You write the Code

Super Contributor
Posts: 644

Re: How To Seprate Character Variable and Numeric Variable

The code is in my reply.

You need to add a data statement, a set statement, and a run statement at the end.

Richard

New Contributor
Posts: 3

Re: How To Seprate Character Variable and Numeric Variable

Dear

I very simple way

data x;  length Result$ 20;
Result='20';output;
Result='>50';output;
Result='123Position';output;
Result='.';output;
Result='.23';output;

run;


data x; set x; v=(verify(Result,'1234567890.'))-1;l=length(trim(result));
length Numeric 8. CHARACTER $20;
if v=l then NUMERIC=result;
  else CHARACTER=result;
run;

You can also investigaute into Regular expression, if you have some experience.

Respected Advisor
Posts: 3,124

Re: How To Seprate Character Variable and Numeric Variable

Your method works if there is no other special scenarios, such as scientific notations, for example "12E4".  You maybe able to exhaust all of the possibilities, but the best practice is to let SAS to worry about it. I vote for Richard's solution.

Haikuo

Ask a Question
Discussion stats
  • 8 replies
  • 372 views
  • 2 likes
  • 5 in conversation