BookmarkSubscribeRSS Feed
SASEnthusiast
Obsidian | Level 7

Hi,

 

I have a variable "Age" with Data as;

 

18-24

15-30

29-54

 

The variable is Character. How can I convert the range to numeric to calculate age range ie. 18-24 is 6 years.

 

5 REPLIES 5
Astounding
PROC Star

One way:

 

age_range = input(scan(age,2,'-'), 3.) - input(scan(age,1,'-'), 3.);

 

You might want to add 1 to the result, if you decide that 18-24 actually spans 7 years not 6.

rkolupoti9001
Calcite | Level 5

data new_age;

set test;

new_age =(INPUT(SCAN(Age, -1), 8.))-(INPUT(SCAN(Age, 1), 8.));

run;

Ksharp
Super User
data have;
input age $20.;
dif=-1*input(resolve(cats('%sysevalf(',age,')')),best.);
cards;
18-24
15-30
29-54
;
run;
PGStats
Opal | Level 21

Very funny @Ksharp!

PG
Ksharp
Super User

Yeah. you can do it with any algorithmic expression.

 

data have;
input age $20.;
dif=input(resolve(cats('%sysevalf(',age,')')),best.);
cards;
18-24
15*30
29/54
;
run;

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!

How to Concatenate Values

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.

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
  • 5 replies
  • 881 views
  • 2 likes
  • 5 in conversation