BookmarkSubscribeRSS Feed
R_Win
Calcite | Level 5
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
1 REPLY 1
Cynthia_sas
Diamond | Level 26
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

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 874 views
  • 0 likes
  • 2 in conversation