1

I'm trying to convert a character variable to a numeric variable, but unfortunately i'm really struggeling. Help would be appreciated!

I keep getting the following error: 'Invalid argument to function INPUT at line 3259 column 17'

Syntax:

Data want;
Set have;
Dosis_num = input(Dosis, best12.);
run;

I have also tried multiplying the variable by 1. This doesnt work either.

The variable looks like this:

Dosis
155
201
2.1
0.8
123.80
12.0
3333.4
00.6

Want:

Dosis_num
155.0
201.0
2.1
0.8
123.8
12.0
333.4
0.6

Thanks alot!

1
  • That code will work (even though it is using the name of a FORMAT where it needs the name of an INFORMAT since SAS will just treat BEST as an alias for the normal numeric informat). So either the values of the character variable you are show are not in the first 12 bytes of the variable (ODS output will hide leading spaces) OR it contains other invisible characters, like tab or linefeed or non-breaking spaces. Commented Jun 13, 2022 at 15:44

2 Answers 2

1

The code will work with the data you show. So either the values in the character variable are not what you think or you are not using the right variable name for the variable.

The code is trying to only use the first 12 bytes of the character variable. Normally you don't need to restrict the number of characters you ask the INPUT() function to use. In fact the INPUT() function does not care if the width of the informat used is larger than the length of the string being read. So just use 32. as the informat since 32 is the maximum width that the normal numeric informat can read. Note that BEST is the name of a FORMAT, if you use it as the name of informat it is just an alias for the normal numeric informat.

If the variable has a length longer than 12 then perhaps there are leading spaces in the variable (note the ODS output displays do not properly display leading spaces) then use the LEFT() function to remove them.

Dosis_num = input(left(Dosis), 32.);
Sign up to request clarification or add additional context in comments.

Comments

0

The typical thing to do here is to find out what's actually in the character variable. There is likely something in there that is causing the issue.

Try this:

data have;
input @1 Dosis $8.;
datalines;
155
201
2.1
0.8
123.80
12.0
3333.4
00.6
;;;;
run;

data check;
  set have;
  put dosis hex32.;
run;

What I get is this:

83         data check;
 84           set have;
 85           put dosis hex32.;
 86         run;
 
 3135352020202020                
 3230312020202020                
 322E312020202020                
 302E382020202020                
 3132332E38302020                
 31322E3020202020                
 333333332E342020                
 30302E3620202020                
 NOTE: There were 8 observations read from the data set WORK.HAVE.
 NOTE: The data set WORK.CHECK has 8 observations and 1 variables.
 NOTE: DATA statement used (Total process time):
       real time           0.01 seconds
       cpu time            0.01 seconds

All those 2020202020 are spaces, which should be there (all strings are space-padded to full length). Period/Decimal Point is 2E, Digits are 3x where x is the digit (because the ASCII for 0 is 30, not because of any other reason). So for example for the last one, 00.6, 30 means zero, 30 means zero, 2E means period, and 36 means 6.

Check to make sure that you don't have any other characters other than digits (3x) and period (2e) and space (20).

The other thing to verify is that your system is set to use . as the decimal separator and not , as many European systems are - otherwise this requires the commaw. informat. You can actually just try the commaw. informat (comma12. is sufficient if 12 is plenty - and don't include anything after the period) as anything that 12. can read in also can be read in by commaw..

3 Comments

Note - this will also show Tom's suggested solution, as there will be 2020202020 etc at the beginning of the PUT if his guess is right and this is right-aligned with spaces on the left.
Thanks! There seem be other characters then 2E, 20 and digits in the string variable. This is not displayed by the table viewer. I'm seeing a lot of ``` a```` . How can I remove these? 31353520202020202020202020202020 3137300A202020202020202020202020 3136340A202020202020202020202020 3136340A202020202020202020202020 3139300A202020202020202020202020 3137300A202020202020202020202020 3232320A202020202020202020202020 3238300A202020202020202020202020 3233342E30380A202020202020202020
Dosis___= compress(Dosis__, ,"kw"); This actually worked!! There were some hidden line feed ('OA'x) characters. Thanks sooooo much. Was trying for two days...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.