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
;

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 328 views
  • 4 likes
  • 3 in conversation