BookmarkSubscribeRSS Feed
Q1983
Lapis Lazuli | Level 10

data have;

input Group $  Value $

datalines;

BBBB $250

CCCC $500

;

run

 

I did the following;

data want;

set have;

val_1 = input(Value,8.);  Also tried other numeric such as 12 to convert to a numeric

run;

My results do not convert to a number.  Instead I get error message such as

Invalid argument to function INPUT

 

Is it because even though it shows as a character it includes dollar signs.  How can I get this to convert to a numeric.

6 REPLIES 6
novinosrin
Tourmaline | Level 20

data have;

input Group $  Value $;

datalines;
BBBB $250
CCCC $500
;

run;

data want;
set have;
val_1 = input(Value,dollar8.);
run;
novinosrin
Tourmaline | Level 20
data want1;
set have;
val_1 = input(Value,comma10.);
run;
Jagadishkatam
Amethyst | Level 16

Alternatively you may try below code 

 

data have;
input Group $  Value $;
datalines;
BBBB $250
CCCC $500
;
run;

data want;
set have;
val_1 = input(compress(Value,,'kd'),best.);
run;
Thanks,
Jag
Allaluiah
Quartz | Level 8

@Q1983 Thank you for posting the $ currency question as I ran into this need just a few moments ago.

 

@Jagadishkatam  Your alternative though looks nice  by stripping chars and keeping digits, but is far slower than the straight forward offered one by @novinosrin  

 

I just tested with about 20 million records and your solution exponentially gets slower as I increase the test with adding number of records.  Could you offer something that is rather better to beat that ?

novinosrin
Tourmaline | Level 20

@Allaluiah  Oh well, it's interesting that you tested the solutions offered here and I appreciate the feedback. I didn't quite expect the diligence by folks on this community for simple solutions. 

 

Ok here you go, this is another one which beat the two variants I posted. So I beat myself . lol

 


data want;
set have;
val_1 = input(substr(value,2),8.);
run;

 

 

novinosrin
Tourmaline | Level 20

And here is my Log report of  my test with 30 Million observations

 

932 /*Compress char and keep digit*/
933 data want;
934 set have;
935 val_1 = input(compress(Value,,'kd'),best.);
936 run;

NOTE: There were 30000000 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.WANT has 30000000 observations and 3 variables.
NOTE: DATA statement used (Total process time):
real time 6.14 seconds
cpu time 6.14 seconds


937 /*extract num from currenct and apply informat */
938 data want3;
939 set have;
940 val_1 = input(substr(value,2),8.);
941 run;

NOTE: There were 30000000 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.WANT3 has 30000000 observations and 3 variables.
NOTE: DATA statement used (Total process time):
real time 2.85 seconds
cpu time 2.73 seconds


942 /*comma informat */
943 data want1;
944 set have;
945 val_1 = input(Value,comma10.);
946 run;

NOTE: There were 30000000 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.WANT1 has 30000000 observations and 3 variables.
NOTE: DATA statement used (Total process time):
real time 3.67 seconds
cpu time 3.57 seconds

 

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