Help using Base SAS procedures

Scanning variables

Reply
Contributor
Posts: 57

Scanning variables

I have a dataset containing various different 'modes' They are labelled ELA_MOD1, ELAL_MOD2.. etc up through 9.  I am hoping to run a code that will go through each of the 9 'modes' to determine if "Y" was entered.   Is there a quick way to run this through SAS other than writing a separate if-then statement for each 'mode'?   Can anyone offer me some assistance?  Thank you

IDGRADEMODE1MODE2MODE3MODE4MODE5MODE6MODE7MODE8MODE9
AB987310YY
TR22911YY
SE950010
MR914812YY
FO231912Y
Super User
Posts: 17,963

Re: Scanning variables

If a Y is any of the fields? Can there be any other entries?

Assuming not there's several options.

num_non_missing=n(of mode1--mode9); (I never remember if its one or two dashes).

mode_found=whichc("Y", of mode1--mode9);

Contributor
Posts: 57

Re: Scanning variables

Yes- the field can also contain "N" or blank.

Super User
Posts: 17,963

Re: Scanning variables

then use the whichc function to find if there is a Y, it will return a number, e.g 3 means mode3 was the one with a Y. 

Super Contributor
Posts: 1,636

Re: Scanning variables

Reeza,

One dash is fine.

Super Contributor
Posts: 1,636

Re: Scanning variables

try:

data have;

input (m1-m5)($);

cards;

n y n y n

Y n n n n

n n n n n

;

data want;

length is_a_y $3;

set have;

is_a_y=ifc(findc(cats(of m1-m5),'y','i'),'YES','NO');

proc print;run;

obs    is_a_y    m1    m2    m3    m4    m5

1      YES      n     y     n     y     n

2      YES      Y     n     n     n     n

3      NO       n     n     n     n     n

Ask a Question
Discussion stats
  • 5 replies
  • 168 views
  • 6 likes
  • 3 in conversation