## First 3 positions of a numeric variable

Regular Contributor
Posts: 229

# First 3 positions of a numeric variable

i have a numeric variable i want the first 3 positions of that variable

data x;
input num;
format num 30.;
cards;
987987899787
768767868755567
578676474545
56756959695998
run;

The out put should be like this

987
768
578
567
SAS Super FREQ
Posts: 9,365

## Re: First 3 positions of a numeric variable

Hi:
The basic procedure is:
1) convert your number to a left-justified character string with the LEFT and PUT functions
2) extract the first3 characters from the left-justified character string with the SUBSTR function
3) if you need the first 3 characters as character, then you're done, But, if you need the first 3 characaters as a number, then you need to convert from a character string to a number using the INPUT function.
[pre]
charvar = left(put(num, 30.)); /* #1 */
first3_char = substr(charvar,1,3); /* #2 */
first3_num = input(first3_char,3.); /* #3 */
[/pre]

You could have done everything in one assignment statement, but I find it useful to break down each step in the transformation, so it's easier to understand.

cynthia
Discussion stats