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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 1007 views
  • 1 like
  • 2 in conversation