BookmarkSubscribeRSS Feed
harrylui
Obsidian | Level 7

good day all,

 

 

Here is some examples in my data set. i want to extract the number after the symbol ~  and store them into a new column

 

Name

EDEKA AKTIVMARKT HARMS QUICKBORN DE~140485089
EDEKA AKTIVMARKT JENA DEU~158665920
EDEKA AM BARBAROSSAPL KOELN DE~148543912

 

ideal demonstration:

 

Name                                                                              name_nbr

EDEKA AKTIVMARKT HARMS QUICKBORN DE        140485089

 

 

Thanks in advance,

Harry

3 REPLIES 3
r_behata
Barite | Level 11
Data have;
input Name $100.;
infile cards truncover;
cards;
EDEKA AKTIVMARKT HARMS QUICKBORN DE~140485089
EDEKA AKTIVMARKT JENA DEU~158665920
EDEKA AM BARBAROSSAPL KOELN DE~148543912
;
run;
data want;
	set have(rename=name=name_);
	Name=scan(name_,1,'~');
	name_nbr=scan(name_,-1,'~');

	drop name_;
run;
Patrick
Opal | Level 21

Or here as a variation to @r_behata if you want to store the digits in a numerical variable.

Data have;
  input Name $100.;
  infile cards truncover;
  cards;
EDEKA AKTIVMARKT HARMS QUICKBORN DE~140485089
EDEKA AKTIVMARKT JENA DEU~158665920
EDEKA AM BARBAROSSAPL KOELN DE~148543912
;
run;

data want;
  set have;
  Number=input(scan(name,-1,'~'),?? best32.);
  Name=scan(name,1,'~');
run;

proc print data=want;
run;

 

Patrick_0-1592367721313.png

 

Jagadishkatam
Amethyst | Level 16

Alternatively with perl regular expression

 

data want;
set have;
name_nbr=prxchange('s/(.*\~)(.*)/$2/',-1,name);
name=prxchange('s/(.*)(\~.*)/$1/',-1,name);
run;
Thanks,
Jag

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 332 views
  • 0 likes
  • 4 in conversation