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
;

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 978 views
  • 4 likes
  • 3 in conversation