BookmarkSubscribeRSS Feed
sas_pkc1
Fluorite | Level 6

 Dear all,

 

I have one column in that character as well as numeric values are there i need to seperate out those value on base of  character and numeric. there is no specific value that we need to seperate out like

ACNB

1980

asjl

8909

8012

dsufjs

 

Like that i have one column want to seperate the character and numeric.

 

Please help

 

Thanks & Regards,

Prashant.

6 REPLIES 6
Kurt_Bremser
Super User
if notdigit(strip(acnb)) > 0
then acnb_char = acnb;
else acnb_num = input(acnb,best.);

notdigit() scans for characters outside 0-9, strip() removes any blanks before notdigit() scans.

sas_pkc1
Fluorite | Level 6

Thanks for idea.

 

but the column ACNB is the character column and number and character are there.

so in strip if my value is 0001 then in final table its coming as 1.

 

that is the problem.

Please suggest if any

RW9
Diamond | Level 26 RW9
Diamond | Level 26

That output is correct, if you take the string 0001 and set this as a numeric value, then number is 1.  there is no such thing as a numeric with preceding zeros.  You can apply a format - zX. where X is the number of elements - but what this is doing is displaying the underlying number 1 as 0001, doesen't change the value, and you would need to apply that format to the whole column.

AlpanaHansaria
Calcite | Level 5

Kurt, your code works fine for digits 0-9. But how about decimals? How would you get the decimal numbers from that same mixed variables also converted to numbers?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Would it not be safer t keep both, for instance in CDISC models you have a character result - which has everything in as its recorded - and also a numeric result where possible.  That way you have best of both worlds, a variable to report as it was, and a numeric to do calculations on?  As for how to do that:

new_col=input(old_col,best.);
Astounding
PROC Star

 The rules for "character" or "numeric" are more complex since a legitimate number can contain a decimal point or a negative sign, but cannot contain embedded blanks.  (We'll leave commas out of the mix here.)  A robust way would be:

 

if acnb > ' ' and input(acnb, ??16.) = .  then acnb_character=acnb;

else acnb_numeric = input(acnb, 16.);

 

You are allowed to use a shorter informat, one that matches the length of your variable.

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
  • 6 replies
  • 6050 views
  • 1 like
  • 5 in conversation