BookmarkSubscribeRSS Feed
Sultana
Calcite | Level 5

Here is a sample data set where column 1 is the ID which has the employees name and number 

I only want the numbers to be outputted 

 

IDDateAmount
Robert, Andrews 222-22-123420191,234.00
Albert, Sanders 123-45-678920182,34.67
Zack, Snyder 111-22-333320201,234.77

 

 

I need the following output

IDDateAmount
222-22-123420191,234.00
123-45-678920182,34.67
111-22-333320201,234.77

 

input ID   : $12

         Year : Year.

        Amount: comma13.

;

 

Please let me know how to only the ID numbers in the output table no names.

 

Thanks

5 REPLIES 5
ChrisNZ
Tourmaline | Level 20

A very simple way would be to use the anydigit() function inside the substr() function .

andreas_lds
Jade | Level 19

There are several ways you can do this:

  1. compress-function to keep only numbers and hyphen.
  2. regular expression with prxchange
  3. anydigit-function to get the position of the first number, then substr to extract the number.

I am sure that after having the a quick look at the documentation of the functions, you are able to solve the problem. If not, please post data in usable form and the code you already have.

Sultana
Calcite | Level 5

can you provide me the compress function code to get only the number and hypen.

andreas_lds
Jade | Level 19

What have you tried? Being able to read the docs and find required information is a necessary skill to learn a programming language.

 

Spoiler
want = compress(have, '-', 'kd');
novinosrin
Tourmaline | Level 20

Your ID has a clear pattern for a SCAN function starting from right to left-

 

 length want_id $15;
 want_id=scan(id,-1,' ');
data have ;
  infile cards truncover expandtabs ;
  input ID $28.	Date	Amount :comma10. ;
  format amount comma10.2 ;
  cards;
Robert, Andrews 222-22-1234	2019	1,234.00
Albert, Sanders 123-45-6789	2018	2,34.67
Zack, Snyder 111-22-3333	2020	1,234.77
;

data want ;
 set have ;
 length want_id $15 ;
 want_id=scan(id,-1,' ') ;
run ;

proc print noobs ; run ;
ID Date Amount want_id
Robert, Andrews 222-22-1234 2019 1,234.00 222-22-1234
Albert, Sanders 123-45-6789 2018 234.67 123-45-6789
Zack, Snyder 111-22-3333 2020 1,234.77 111-22-3333

 

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