BookmarkSubscribeRSS Feed
TJ87
Obsidian | Level 7

Here's a scenario:

data have;

  input id x $;

  cards;

1  a

2  b

3  a or b

;

run

data want;

  input id x $;

  cards;

1  a

2  b

3  a

3  b

;

run;

Could someone please suggest code to get 'data want' from 'data have'? Thanks.

3 REPLIES 3
RichardinOz
Quartz | Level 8

Here is a one step solution

data have;

  Infile cards missover ;

  input id x $ @ ;

  output ;

  input @'or' x ;

  If not missing(x) then output ;

  cards;

1  a

2  b

3  a or b

;

run ;

Richard

Ksharp
Super User
data have;
  input id x $10.;
  cards;
1  a
2  b
3  a or b
;
run;
data want(drop=i x);
 set have;
 length y $ 10;
 do i=1 to countw(x);
  y=scan(x,i);
  if y ne 'or' then output;
 end;
run;
  

Xia Keshan

stat_sas
Ammonite | Level 13

data want(drop= i x rename=(y=x));

set have;

i=1;

do while (scan(x,i,'or ') ne ' ');

id=_n_;

y=scan(x,i,'or ');

output;

i=i+1;

end;

run;

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 25. 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
  • 600 views
  • 0 likes
  • 4 in conversation