DATA Step, Macro, Functions and more

Extracting zero or more codes from a string

Accepted Solution Solved
Reply
Contributor
Posts: 37
Accepted Solution

Extracting zero or more codes from a string

Hi,

 

I have a field that contains (ideally) a single three-number code, e.g., 123. Variations on this ideal case include:

  • missing data
  • ;
  • 123;
  • 123; 123
  • 123 456
  • 123; 456
  • 123; 456;
  • 123;  456;

I think that's about it. Each row (observation) contains other fields. When my code field is empty, I'd like to just ignore that observation. If there's only one code, I'll save (output) the observatoin. If there's two unique codes, I'd like to output the observation twice, once for each code. If the code is a lot trickier for the 123; 123 case, I can live with a duplicate. I was thinking of using some of the prx functions. Any suggestions?

 

Thanks! Bruce


Accepted Solutions
Solution
‎09-13-2016 01:04 PM
Super User
Posts: 10,552

Re: Extracting zero or more codes from a string

[ Edited ]

Is this a text file you are reading or is this "field" already a SAS variable?

 

By "ignore" do you mean remove from the data set?

 

Scan should work just fine if this is already a SAS varaible.

 

data have;
   infile datalines truncover;
   input code $ 1-15 ;
datalines4;
 
;
123;
123; 123
123 456
123; 456
123; 456;
123;  456;
;;;;
run;

data want;
   set have;
   if countw(code,' ;,')>0 then do i=1 to (countw(code,' ;,'));
      NewCode=scan(code,i,' ;,');
      output;
   end;
   drop i;
run;

 

View solution in original post


All Replies
Solution
‎09-13-2016 01:04 PM
Super User
Posts: 10,552

Re: Extracting zero or more codes from a string

[ Edited ]

Is this a text file you are reading or is this "field" already a SAS variable?

 

By "ignore" do you mean remove from the data set?

 

Scan should work just fine if this is already a SAS varaible.

 

data have;
   infile datalines truncover;
   input code $ 1-15 ;
datalines4;
 
;
123;
123; 123
123 456
123; 456
123; 456;
123;  456;
;;;;
run;

data want;
   set have;
   if countw(code,' ;,')>0 then do i=1 to (countw(code,' ;,'));
      NewCode=scan(code,i,' ;,');
      output;
   end;
   drop i;
run;

 

Contributor
Posts: 37

Re: Extracting zero or more codes from a string

Hi. The field is already a SAS variable, one of many variables in a data set. Yes, "ignore" means remove from the data set. I will try your code. Thank you!
Contributor
Posts: 37

Re: Extracting zero or more codes from a string

Works great. Thank you!
☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 253 views
  • 1 like
  • 2 in conversation