BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
brucehughw
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

3 REPLIES 3
ballardw
Super User

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;

 

brucehughw
Obsidian | Level 7
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!
brucehughw
Obsidian | Level 7
Works great. Thank you!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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