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
SAS Super FREQ
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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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