How to find the length of a numeric variable using PROC SQL

I have a dataset with a column of phone numbers. I want to filter this dataset using PROC SQL WHERE, the length of the numbers is at least 7 digits.

In regular SQL, I can simply apply the length function around a number, and it works, however in SAS, it will not let me apply it to a numeric column.

My first instinct is to convert the column to a character and then find its length, but I can only specify the size when I use the function put. However, I don’t even know the largest size of my numbers, because I can’t calculate the length!

How to find the length of a numeric value in SAS using PROC SQL?

+4
source share
3 answers

Since you did not post the sample data, so I created it for myself

Creating a sample dataset. Taking phonenumas numericmuch as in your case.

data test;
infile datalines;
input phonenum : 8.;
datalines;
123
1234
12345
123456
1234567
12345678
123456789
12345678910
;
run;  

You are right in the approach, if you want to count the number of digits, you need to convert it to charby following these steps:

  • Convert numericphonenum to char. Although it is obvious that the number of digits will not be more than 32, but if you want to increase the score.
  • Using compressfor stripempty symbols
  • Using a function lengthto count the number of digits
  • proc sql\SAS where , proc sql , calculated .

proc sql;
select length(compress(put(phonenum,32.))) as phonelen from test where calculated phonelen > 6;
quit;

, datasteps (SAS), :

data _null_;
set test;
phonelen=length(compress(input(phonenum,$32.)));
if phonelen > 6;
put phonelen=;
run;
+6

SAS, length() ().

:

proc sql;
    select length(put(x,32. -l)) from test;
quit;

. -l ( ). 32 ( , ) 10 ( - ..).

,

numvar ge 1000000

, ?

, , . 7- 7 , 8 - , 7 , .

0

I would suggest using magic:

log10(numericphonenumber)>6
-2
source

All Articles