BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ambadi007
Quartz | Level 8

Hi Team,

 

I have to convert a variable in to numeric , but I am getting a warning "Invalid argument to function Input"

 

I have used var1=input(var,best.);

 

The variable contains data like below

 

3

4

non-reactive

5

non-reactive

6

7

etc....

 

the warning is coming for the non-reactive. how to handle this to make this in to numeric 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@ambadi007 wrote:

Hi Team,

 

I have to convert a variable in to numeric , but I am getting a warning "Invalid argument to function Input"

 

I have used var1=input(var,best.);

 

The variable contains data like below

 

3

4

non-reactive

5

non-reactive

6

7

etc....

 

the warning is coming for the non-reactive. how to handle this to make this in to numeric 


What number to you want to convert 'non-reactive' into?

If you want any value that is not a number to just become missing without SAS complaining about it you can add the ?? prefix to the informat.  

var1=input(var,??32.);

Note that trying to use BEST as the name of an informat will just revert to using the normal numeric informat.  There is nothing better about using best, the only thing it adds is confusion. The maximum length string that the normal numeric informat can handle is 32 bytes.

View solution in original post

6 REPLIES 6
SASKiwi
PROC Star

A SAS numeric variable can only contain numbers or a missing value. Either switch to using a character variable that can handle both numbers and characters (like you have already) or record "non-responsive" in a separate character variable.

jvdl
Obsidian | Level 7

You could always try compressing only the digits into a numeric variable as follows:

 

data have;
	length var $15.;
	input var $;
	cards;
3
4
non-reactive
5
non-reactive
6
7	
;
run;

data want;
	set have;
	
	char_digits = compress(var, '', 'kd');
	num_digits = input(char_digits, best.);
run;

Be warned that values like "non-reactive1" will be translated to 1. Inspect your data to make sure you get the right results. You could work around that by using the ANYALPHA function and ignoring values that have characters in them.

 

data want;
	set have;
	
	any_digit = anydigit(var);
	any_char = anyalpha(var);
	
	if not (any_char) then do;
		char_digits = compress(var, '', 'kd');
		num_digits = input(char_digits, best.);
	end;
run;
Patrick
Opal | Level 21

A numeric variable can only store digits. Below how you could replace alphanumeric values with digits.

data have;
	length var $15.;
	input var $;
	cards;
3
4
non-reactive
5
non-reactive
6
7	
;

proc format;
  invalue recode(upcase)
    'NON-REACTIVE' = 0
    other = _same_
    ;
quit;

data want;
  set have;
  var_recoded=input(var,recode.);
run;

proc print;
run;
  

 

FreelanceReinh
Jade | Level 19

Hi @ambadi007,

 

If there's only a small, fixed set of possible non-numeric values, you could use formatted special missing values to store everything in a numeric variable.

 

Example:

 

/* Prepare informat and format for possible non-numeric values */

data infmt;
retain fmtname 'labi' type 'I' default 20;
input start :$12. label :$2.;
cards;
non-reactive .N
reactive     .R
unknown      .U
;
run;

data fmt;
set infmt(rename=(label=start start=label) drop=type);
fmtname='labf';
run;

/* Create informat LABI and format LABF as defined above */

proc format cntlin=infmt;
proc format cntlin=fmt;
run;

/* Apply informat and format to values */

data have;
input value labi.;
format value labf14.;
cards;
3
4
non-reactive
5
non-reactive
6
7
unknown
reactive
;

proc print data=have noobs;
run;

Output:

 

value

             3
             4
non-reactive
             5
non-reactive
             6
             7
unknown
reactive
Ksharp
Super User

What do you want ?

 

 var1=input(var, ?? best.);

Tom
Super User Tom
Super User

@ambadi007 wrote:

Hi Team,

 

I have to convert a variable in to numeric , but I am getting a warning "Invalid argument to function Input"

 

I have used var1=input(var,best.);

 

The variable contains data like below

 

3

4

non-reactive

5

non-reactive

6

7

etc....

 

the warning is coming for the non-reactive. how to handle this to make this in to numeric 


What number to you want to convert 'non-reactive' into?

If you want any value that is not a number to just become missing without SAS complaining about it you can add the ?? prefix to the informat.  

var1=input(var,??32.);

Note that trying to use BEST as the name of an informat will just revert to using the normal numeric informat.  There is nothing better about using best, the only thing it adds is confusion. The maximum length string that the normal numeric informat can handle is 32 bytes.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 3086 views
  • 4 likes
  • 7 in conversation