BookmarkSubscribeRSS Feed
vallsas
Pyrite | Level 9

hi,

i have 12.2.3.4

           12.33.44.55

            122.333.333.55

 

variable having data like above would like to extract first 3 parts. by using substring it is not possible is there any other functions in sas to get result like below

12.2.3

12.33.44

122.333.333

2 REPLIES 2
Patrick
Opal | Level 21
data have;
  infile datalines truncover;
  input string $20.;
  datalines;
12.2.3.4
12.33.44.55
122.333.333.55
12.2
;

data want;
  set have;
  if 0 then string_want=string;
  string_want=catx('.',scan(string,1,'.'),scan(string,2,'.'),scan(string,3,'.'));
run;

proc print data=want;
run;

Patrick_0-1665054244878.png

 

FreelanceReinh
Jade | Level 19

Hi @vallsas,


@vallsas wrote:

... would like to extract first 3 parts. by using substring it is not possible ...


Why not?

data want;
input have :$30.;
want=substr(have,1,findc(have,'.','b')-1);
cards;
12.2.3.4
12.33.44.55
122.333.333.55
;

Or, if the number of "parts" varies:

data want(drop=pos len);
input have :$30.;
call scan(have,3,pos,len);
want=substr(have,1,pos+len-1);
cards;
12.2.3.4.5
12.33.44.55.66.77
122.333.333.55.6.77.888.9
;

 

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

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 1075 views
  • 4 likes
  • 3 in conversation