BookmarkSubscribeRSS Feed
thanikondharish
Calcite | Level 5

data s ;

input id $ result $ ;

cards ;

111   482

112   854

113    neg

114    pos

115    45.5

;

 

extract numeric values and character values from result variable?

4 REPLIES 4
novinosrin
Tourmaline | Level 20
data s ;

input id $ result $ ;

cards ;
111   482
112   854
113    neg
114    pos
;

data want;
set s;
n=compress(result,' ','kd');
c=compress(result,' ','d');
run;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Show what the output should look like.  Do you want a numeric column with those which are numeric converted?  Would you like separate datasets, etc?  For columns:

data want;
set s;
if not anyalpha(result) then num_var=input(result,best.);
run;
s_manoj
Quartz | Level 8

Hi,

Check below code

 

data want;
set s;
if input(result, best.) then numeric = result;
else char = result;
run;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You have warnings in that code though about converting text to numeric, actually I fell into the same trap as I forgot that ifn() will do the conversion regardless of the if part.  I updated my code to avoid this.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 1035 views
  • 0 likes
  • 4 in conversation