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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 932 views
  • 2 likes
  • 5 in conversation