Learning SAS? Welcome to the exclusive online community for all SAS learners.

Help with character functions?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 19
Accepted Solution

Help with character functions?

IS there a character function that counts the non missing across rows. Like we have N(of var1-varn). Do we have something like this for character variables?


Accepted Solutions
Solution
‎08-08-2014 12:35 PM
Super User
Super User
Posts: 6,502

Re: Help with character functions?

Doesn't look like you have a two dimensional array at all, but instead two different arrays.

%let principal=var1-var3 ;

%let address=var4-var6 ;

data want;

  set have;

  array p &principal;

  array a &address ;

  pmiss = cmiss(of &principal);

  amiss = cmiss(of &address);

  pnonmiss = dim(p) - pmiss;

  anonmiss = dim(a) - amiss;

  put _n_=

     /(pmiss pnonmiss &principal) (=)

     /(amiss anonmiss &address) (=)

     /

  ;

run;


_N_=1

pmiss=3 pnonmiss=0 var1= var2=  var3=

amiss=3 anonmiss=0 var4= var5=  var6=

_N_=2

pmiss=1 pnonmiss=2 var1=A var2= var3=C

amiss=2 anonmiss=1 var4= var5=E var6=

NOTE: There were 2 observations read from the data set WORK.HAVE.

View solution in original post


All Replies
Regular Contributor
Posts: 244

Re: Help with character functions?

Depending on your purpose, there are a few ways to do this.  Two examples: use CMISS to determine number of missing then subtract from total (if known), or use CATX to concatenate all values with some separator that cannot occur in the data, then count number of separators.  (CATX won't add a character value that is msising).

data have;

input (x1-x4) ($);

datalines;

. A . B

A B C D

. . . .

A B . D

. A B .

;;;;

run;

data want;

set have;

str = catx('|',of x1-x4);

count = ifn(missing(str),0,countc(str,'|')+1);

count2 = 4- cmiss(of x1-x4);

run;

Respected Advisor
Posts: 3,777

Re: Help with character functions?

Arrays make good "SAS Variable Lists" that can also be defined with a "SAS Variable List"   e.g.  Array c

  • _character_;
  • Then you can ask how many with DIM.

    data have;
       array x[4] $;
       input x
  • ;
  •    count = dim(x)-cmiss(of x
  • );
      
  • datalines;
    . A . B
    A B C D
    . . . .
    A B . D
    . A B .
    ;;;;
       run;

    Occasional Contributor
    Posts: 19

    Re: Help with character functions?

    Thank you so much snoopy369 data_null. Actually what I am looking into is:

    data test2;

    set test1;

    array x{2}{3} $ var1-var3 var4-var6;

    run;

    the var1-var3 are of character type and have values with one letter, example:

    var1 var2 var3

    C      .     .

    .     .     X

    .     P     .

    similarly there are variables of similar format var4-var6.

    Do you think, in order to obtain number of non zeros in each row, I can still apply this function?

    Regular Contributor
    Posts: 244

    Re: Help with character functions?

    You can use arrays like this:

    cmiss(of arrayname

  • )
  • I can't test right now and don't remember if two dimensional arrays are one

  • or are
  • .
  • Occasional Contributor
    Posts: 19

    Re: Help with character functions?

    actually I think if I do the following it might work:

    do i = 1 to 2;

              var_count(i) = cmiss(of x{i}{1},x{i}2},x{i}{3});

    end

    Please suggest.

    Regular Contributor
    Posts: 244

    Re: Help with character functions?

    Well, I think (of x

  • ) should work.
  • Occasional Contributor
    Posts: 19

    Re: Help with character functions?

    I am not able to resolve the issue. This gives me a negative number. I only want to report number of non missing. Please can you suggest.

    Super User
    Super User
    Posts: 7,430

    Re: Help with character functions?

    Well, I am sure someone will come up with a fancy way of doing it with arrays, but I still like the good 'ol code generator:

    data have; 
      attrib a b c d format=$1.;
      infile datalines missover dlm="," dsd;
      input a $ b $ c $ d $;
    datalines;
    ,A,,B
    A,B,C,D
    ,,,
    A,B,,D
    ;
    run;

    data _null_;
      set sashelp.vcolumn (where=(libname="WORK" and MEMNAME="HAVE")) end=last;
      if _n_=1 then call execute('data want; 
                                                   set have;
                                                   attrib mycount format=best.;
                                                   mycount=0;');
      call execute('if '||strip(name)||' ne "" then mycount=mycount+1;');
      if last then call execute('run;');
    run;
     

    Respected Advisor
    Posts: 3,777

    Re: Help with character functions?

    It doesn't work that way.  What you have shown doesn't make sense to me.

    Start over and show what you HAVE and what you WANT.

    Super User
    Super User
    Posts: 6,502

    Re: Help with character functions?

    What do you mean by ROW?  Normally that means the current observation, but by introducing a multi-dimensional array definition perhaps you want row to mean something different?

    Occasional Contributor
    Posts: 19

    Re: Help with character functions?

    Thank you all for your input.

    Stat@SAS I applied the same logic too but when I want the frequency counts the variable no_miss, i am not getting anything.

    let me explain again what I am looking for:

    Example:

    /**2-d array with dimensions m=2 and n=3**/

    /***I have two groups of variables being referred like change in school principal as one group var1-var3 which is tracked over one quarter and change in school address is tracked in another quarter var4-var6.**/

    data want;

         set have;

    array y{m}{n} var1-var3 var4-var6;

    array no_miss(3) 3;

    no_miss1 =0; no_miss2=0; no_miss3=0;

    do i =1:m;

              if y{i,1} ne ' ' then no_miss(i) = no_miss(i) + 1;

              if y{i,2} ne ' ' then no_miss(i) = no_miss(i) + 1;

              if y{i,3} ne ' ' then no_miss(i) = no_miss(i) + 1;

    end;

    run;

    Do you think this will record the count for each group or not?

    Thanks.

    Trusted Advisor
    Posts: 1,204

    Re: Help with character functions?

    Hi,

    So if you want to no_miss for two different set of variables then try this modified code. First set contains variables x1,x2 while the second set is based on variables x3,x4. Hope this will help.

    data have;
    infile datalines missover;
    input (x1-x4) ($);
    datalines;
    . A . B
    A B C D
    . . . .
    A B . D
    . A B .
    ;;;;
    run;

    data want (drop=j);
    set have;
    array x{*} x1-x2;
    array y{*} x3-x4;
    non_miss1=0;
    non_miss2=0;
    do i=1 to dim(x);
    if x{i} ne ' ' then non_miss1+1;
    end;
    do j=1 to dim(y);
    if y{j} ne ' ' then non_miss2+1;
    end;
    run;

    Occasional Contributor
    Posts: 19

    Re: Help with character functions?

    Hi Stat_Sas,

    Thank you for replying to my question. Actually i tried that too. But I do not why the frequency counts of the no miss variable are not appearing. I am now trying what Tom said.

    Trusted Advisor
    Posts: 1,204

    Re: Help with character functions?

    Not sure, can you please share generated output?

    🔒 This topic is solved and locked.

    Need further help from the community? Please ask a new question.

    Discussion stats
    • 18 replies
    • 767 views
    • 0 likes
    • 6 in conversation