Hello
I want to create a binary variable that check if char var contain only numbers.
I saw this code in google search
isnumber = not missing(input(X_Char,?? 32.));
and I want to understand it please.
What is the meaning of ??
I see that also this code work well
isnumber = not missing(input(X_Char,best.));
data have;
input X_Char $;
isnumber = not missing(input(X_Char,?? 32.));
datalines;
3.22
4..2
N/A
1.23e7
(a1) 1234
(a2) 2000
8000
Good
;
Run;
You can also use the NOTDIGIT Function
@Kathryn_SAS wrote:
You can also use the NOTDIGIT Function
Yes I thought of that, but NOTDIGIT fails for a character string like 3.22
The ? and ?? modifiers suppress error messages.
32 is the maximum number of characters that the normal numeric informat can handle.
But if you don't specify the width to use for an informat then SAS will default to 12. So your two statements are not equivalent as the first one reads the first 32 bytes of the character variable and the second only the first 12.
Note that BEST is the name of a FORMAT that tries to find the best way to represent a number in a fixed number of characters. There is no equivalent meaning of the best way to represent a number as a number so using BEST as the name of an informat is just confusing. If you do use it that way then SAS will just take at as an alias for the normal numeric informat, the one you used in your first statement.
You could try checking if the string only contains digits. Or perhaps digits and period. Or perhaps also include hyphen and plus sign. But that would yield false positives for values with multiple periods. And it would yield false negatives for values in scientific notation. Note that the BEST format will generate strings in scientific notation when it has to.
One thing you might want to consider is using the COMMA (or perhaps COMMAX) informat instead or the normal numeric informat. That will accept strings with commas in them (and also dollar signs).
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.
Ready to level-up your skills? Choose your own adventure.