BookmarkSubscribeRSS Feed
SushilKumar
Calcite | Level 5

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

8 REPLIES 8
LinusH
Tourmaline | Level 20

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
RichardinOz
Quartz | Level 8

Warning messages can be suppressed using ??

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

If numeric = . then character = result ;

Richard

LinusH
Tourmaline | Level 20

Forgot that:smileyblush:.

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

Data never sleeps
RichardinOz
Quartz | Level 8

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

SushilKumar
Calcite | Level 5

Could You write the Code

RichardinOz
Quartz | Level 8

The code is in my reply.

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

Richard

FabienLACREUSE
Fluorite | Level 6

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.

Haikuo
Onyx | Level 15

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 8 replies
  • 1667 views
  • 2 likes
  • 5 in conversation