Hi., I m beginner of Sas base so help me out

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

Hi., I m beginner of Sas base so help me out

Data Sample;

input var $20.;

datalines;

car123personal

truck456commercial

bike789racing

;

run;

I need to place this argument into three variables and output as

Vehicle   Number  Purpose

car          123        personal

truck        456       commercial

bike         789       racing


Accepted Solutions
Solution
‎08-02-2013 03:31 AM
Frequent Contributor
Posts: 85

Re: Hi., I m beginner of Sas base so help me out

Posted in reply to NagendraBS

PRX functions to the rescue.

Try this...

Data Sample;

   input var $20.;

   length var2 $25;

   var2 = prxchange('s/([a-z]+)(\d+)([a-z]+)/$1 $2 $3/',-1,var);

   vehicle = scan(var2,1);   number = input(scan(var2,2),3.);

   purpose = scan(var2,3);

datalines;

car123personal

truck456commercial

bike789racing ;

run;

The prxchange function is being used here to insert delimiters between the elements of the data, which the SCAN function can easily chop up.

You will need to read the documentation to see just how the PRXCHANGE function works!!

View solution in original post


All Replies
Frequent Contributor
Posts: 78

Re: Hi., I m beginner of Sas base so help me out

Posted in reply to NagendraBS

i there any delimiter ???

Occasional Contributor
Posts: 13

Re: Hi., I m beginner of Sas base so help me out

Posted in reply to TarunKumar

nope.. no delimeter to separate itz complete single character argument. we need to split it into 3 variables

Solution
‎08-02-2013 03:31 AM
Frequent Contributor
Posts: 85

Re: Hi., I m beginner of Sas base so help me out

Posted in reply to NagendraBS

PRX functions to the rescue.

Try this...

Data Sample;

   input var $20.;

   length var2 $25;

   var2 = prxchange('s/([a-z]+)(\d+)([a-z]+)/$1 $2 $3/',-1,var);

   vehicle = scan(var2,1);   number = input(scan(var2,2),3.);

   purpose = scan(var2,3);

datalines;

car123personal

truck456commercial

bike789racing ;

run;

The prxchange function is being used here to insert delimiters between the elements of the data, which the SCAN function can easily chop up.

You will need to read the documentation to see just how the PRXCHANGE function works!!

Occasional Contributor
Posts: 13

Re: Hi., I m beginner of Sas base so help me out

Posted in reply to JerryLeBreton

Thanks a lot JerryLeBreton........ Itz working

Super Contributor
Posts: 644

Re: Hi., I m beginner of Sas base so help me out

Posted in reply to NagendraBS

Here is a solution that does not use PRX functions and is perhaps easier to understand and maintain:

Use the numbers to act as a delimiter between the first and third variables

     vehicle = scan (var, 1, '1234567890') ;

     purpose = scan (var, 3, '1234567890') ;

Then throw away the non numeric characters

     number = compress (var,,'KD') ;

Richard

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 244 views
  • 4 likes
  • 4 in conversation