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

 

 

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

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