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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 505 views
  • 0 likes
  • 2 in conversation