sas >> substr function with numeric argument

by olga eyre » Thu, 19 May 2005 15:04:32 GMT

Would love to know why substr() accepts a numeric argument and produces the
result below:


data one;;

Code='('!! substr(Phonenumber,1,3) !!')';

proc print;


Obs Phonenumber Code
1 3125551212 ( 3)

sas >> substr function with numeric argument

by Joep » Thu, 19 May 2005 15:45:53 GMT


That' s due to the automatic conversion to character. The character
value is right aligned. See the excerpt in my log:

327 data one;;
328 Phonenumber=3125551212;
329 txt_number = put(phonenumber, best.);
330 lg = length(txt_number);
331 lg2 = length(left(txt_number));
332 Code='('!! substr(Phonenumber,1,3) !!')';
333 Code2 = '('!! substr(left(put(Phonenumber, best.)),1,3) !!')';
334 put (_all_) (=);
336 run;

NOTE: Numeric values have been converted to character values at the
places given by: (Line):(Column).
Phonenumber=3125551212 txt_number=3125551212 lg=12 lg2=10 Code=( 3)

Regards, Joep

sas >> substr function with numeric argument

by ghellrieg » Thu, 19 May 2005 17:23:45 GMT

SUBSTR does not accept a numeric argument, but SAS is converting nearly
everything to the appropriate type, before using it. Numerics are always
shifted to the right, so obviously the conversion brings 2 leading blanks
and substr cuts out 2 blanks and the "3".
Doing the conversion yourselve, the results might be better.
Also adding compress or left to your code the results are different:

data one;;

Code='('!! substr(compress(Phonenumber),1,3) !!')';

proc print; run;


Obs Phonenumber Code

1 3125551212 (312)

sas >> substr function with numeric argument

by Richard A. DeVenezia » Thu, 19 May 2005 19:16:53 GMT

When SAS encounters a number, when a character is expected, it will
automatically convert the number to a character string using the BEST12.

What happens when you do

code1 = substr (put(phonenumber,12.),1,3);
code1 = substr (put(phonenumber,10.),1,3);
code1 = substr (phonenumber,3,5);

A better extraction might be

S = put (phonenumber,10.-L);
p = verify (S,'0123456789');
if length(S)=10 and p=0 then
code = substr(S,1,3);
code = 'n/a';

Similar Threads

1. Fw: Numeric argument for Sum Function -sorry

2. Numeric argument for Sum Function

Error log below  requires a numeric argument for sum function  while all  values are numeric according to the Proc contents:
399  Proc SQL ;
400  Create Table PR As
401  Select region, sum(patients) as sumtot_patient,
402                               sum(covered)  as sumtot_covered,
403                               sum(member_months) as sumtot_mm
404  From bhi.PR_sub
405  Group By region;
ERROR: The SUM summary function requires a numeric argument.
ERROR: The following columns were not found in the contributing tables: member_months.
406  Quit ;
                            #    Variable                Type    Len
                            4    allowed                 Num       8
                            3    covered                 Num       8
                            2    patients                 Num       8
                            1    region                   Char     23
What I am doing wrong? How can I fix my code?
Could you please give me a hand? Thank you in advance,

3. SUBSTR with numeric data

4. NOTE: Invalid argument to function INPUT

I am bringing in some survey data from survey monkey, so it is all in text
format.  To make it easier to work with I'm trying to switch everything to
numeric.  I have recoded all of my variables in the same manner as the
example below, but each time I try to run it I get an error that says:
NOTE: Invalid argument to function INPUT at line 11070 column 7.

I have gone through the frequencies of my old variables and they have all
been changed, although the freq tables look a little funny with the
variable values a little lower than the descriptive statistics on the page
(I think this is due to the fact that they are numbers but in a character

I have seen the suggestions that others have posted regarding this error,
that there is still something character that is left in the variable, but
can't find anything.  Does anyone have any suggestions??

Thanks so much,

**here is a sample of the way I am recoding to numeric and then formatting
into a numeric variable:

data apa; set surveys.apa; options nofmterr;
If In_what_state_do_you_work____che = 'Connecticut' then
In_what_state_do_you_work____che = 1;
else if In_what_state_do_you_work____che = 'Maine' then
In_what_state_do_you_work____che = 2;
else if In_what_state_do_you_work____che = 'Massachusetts' then
In_what_state_do_you_work____che = 3;
else if In_what_state_do_you_work____che = 'New Hampshire' then
In_what_state_do_you_work____che = 4;
else if In_what_state_do_you_work____che = 'Rhode Island' then
In_what_state_do_you_work____che = 5;
else if In_what_state_do_you_work____che = 'Vermont' then
In_what_state_do_you_work____che = 6;
else In_what_state_do_you_work____che = 0;
State = input(In_what_state_do_you_work____che,best4.);

5. invallid argument to INPUT function

6. Invalid argument to function DATEJUL...

Hello All.

This is the entire error message:
Invalid argument to function DATEJUL at line 14 column 14.

1 -  - -5                        4
CHAR  .&..D ..SYA4..=BC
ZONE  0500C400EECF03B
NUMR  100040012814047

                                                                DATA DCOL;                    =20
                                                                INFILE DCOL;                  =20
                                                                        INPUT @05  DCURCTYP $CHAR2. @;=20
                                                                IF DCURCTYP =3D: 'D';           =20
                                                                INPUT @25  DCDDSNAM $CHAR44.  =20
                                                                          @79  DCDVOLSR $CHAR6.   =20
                                                                        @89  DCDALLSP PIB4.     =20
                                                                          @93  DCDUSESP PIB4.     =20
                                                                           @105 DCDCREDT PD4.      =20
                                                                           @113 DCDLSTRF PD4.;     =20
                                                                FORMAT CRDATE MMDDYY8.;

Why am I getting the above message?  Why is SAS looking at DATJUL in =
column 14 when the dates are in columns 105 and 113?
TIA, ~~Carol

7. ZIPSTATE function, turn off invalid argument NOTE?

8. varying the number of arguments in a datastep function

I am writing a macro in which I want to determine the maximum value of
a set of variables from a set of merged datasets where the number of datasets
can vary. The gist of my problem can be seen in this example:

%macro test(N);
data total;
*% generate and merge the example datasets here;
%do I = 1 %to &N.;
	data rand&I.;
		do c = 1 to 10;
			a&I. = uniform(-1);
	data total;
		merge total rand&I.;

*% here is the problem...I want to be able to 
vary the number of arguments in max automatically;

data max;
	set total;
	max = max(a1, a2, a3, a4, a5);
proc print;run;

In other words, how can I calculate the max of a varying number of
arguments in this macro, namely a1, a2, ...aN, where N is any positive