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

 

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 383 views
  • 0 likes
  • 2 in conversation