### sas >> substr function with numeric argument

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

Thanx
Eric
XXXX@XXXXX.COM

data one;;
Phonenumber=3125551212;

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

proc print;

run;

Obs Phonenumber Code
1 3125551212 ( 3)

### sas >> substr function with numeric argument

Eric,

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_) (=);
335
336 run;

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

Regards, Joep

### sas >> substr function with numeric argument

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;;
Phonenumber=3125551212;

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

proc print; run;

-->>

Obs Phonenumber Code

1 3125551212 (312)

### sas >> substr function with numeric argument

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

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);
else
code = 'n/a';

```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?

Irin
```

```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
format).

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,
Alison

**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.);
```

```Hello All.

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

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

CODE:
=20
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
CRDATE=3DDATEJUL(DCDCREDT);
FORMAT CRDATE MMDDYY8.;
RUN;

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
=20
=20
```

```Hi,
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;
run;
*% generate and merge the example datasets here;
%do I = 1 %to &N.;
data rand&I.;
do c = 1 to 10;
a&I. = uniform(-1);
output;
end;
run;
data total;
merge total rand&I.;
run;
%end;

*% 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);
run;
proc print;run;
%mend;
%test(5);

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
integer.

Thanks
Tomislav
```