BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SP01
Obsidian | Level 7
data Have;
input col1 $ col2 $ col3 $ col4 $ col5 $ col6 $ col7 $ col8 $ col9 $ col10 $;
cards;
PM MM JM MM PM PB . . PM .
PM MM JM MM PM PB JM . . .
PM MM JM MM PM MB PM MM . .
PM MM JM MM PM PM MM MB . .
PM MM JM MM PM PM MM PB . .
;

 Hello all, I was not clear in my details and goal in my last post. So, I am reposting this again.

 

The data look like what I have in 'Have' above.

 

Want:

I need to find and flag records that have any value occurring after 'PB' or 'MB', if there is any non-missing value occurring after values 'PB' or 'MB' horizontally, I want to flag it.

                                        flag 
PM MM JM MM PM PB . . PM .               1
PM MM JM MM PM PB JM . . .               1
PM MM JM MM PM MB PM MM . .              1
PM MM JM MM PM PM MM MB .                0
PM MM JM MM PM PM MM PB .                0

Thank you for your time on this.

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

I below giving you the desired outcome?

data have;
  infile datalines truncover;
  input col1 $ col2 $ col3 $ col4 $ col5 $ col6 $ col7 $ col8 $ col9 $ col10 $;
  datalines;
PM MM JM MM PM PB . . PM .
PM MM JM MM PM PB JM . . .
PM MM JM MM PM MB PM MM . .
PM MM JM MM PM PM MM MB . .
PM MM JM MM PM PM MM PB . .
PB MB
MM JM MM PM PM MM
PM MM JM MM PM PM MM MM . PB
MM MM MM MM PM PM MM MM MM MM
;

data new(drop=_:);
  set have;
  array _cols {*} col:;
  flag=0;
  _flag=0;
  do _i=dim(_cols) to 1 by -1;
    if _cols[_i] in ('MB','PB') then
      do;
        flag=_flag;
        leave;
      end;
    if not missing(_cols[_i]) and _cols[_i] not in ('MB','PB') then _flag=1;
  end;
run;

proc print;
run;

Patrick_0-1679181368107.png

 

 

View solution in original post

2 REPLIES 2
Patrick
Opal | Level 21

I below giving you the desired outcome?

data have;
  infile datalines truncover;
  input col1 $ col2 $ col3 $ col4 $ col5 $ col6 $ col7 $ col8 $ col9 $ col10 $;
  datalines;
PM MM JM MM PM PB . . PM .
PM MM JM MM PM PB JM . . .
PM MM JM MM PM MB PM MM . .
PM MM JM MM PM PM MM MB . .
PM MM JM MM PM PM MM PB . .
PB MB
MM JM MM PM PM MM
PM MM JM MM PM PM MM MM . PB
MM MM MM MM PM PM MM MM MM MM
;

data new(drop=_:);
  set have;
  array _cols {*} col:;
  flag=0;
  _flag=0;
  do _i=dim(_cols) to 1 by -1;
    if _cols[_i] in ('MB','PB') then
      do;
        flag=_flag;
        leave;
      end;
    if not missing(_cols[_i]) and _cols[_i] not in ('MB','PB') then _flag=1;
  end;
run;

proc print;
run;

Patrick_0-1679181368107.png

 

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 2 replies
  • 761 views
  • 0 likes
  • 2 in conversation