Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- Learn SAS
- /
- Analytics U
- /
- Help with character functions?

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-07-2014 05:19 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to sas_kms

08-08-2014 12:35 PM

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.

All Replies

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to sas_kms

08-07-2014 05:30 PM

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to snoopy369

08-07-2014 05:45 PM

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

Then you can ask how many with DIM.

array x[

input x

. A . B

A B C D

. . . .

A B . D

. A B .

;;;;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to data_null__

08-07-2014 09:54 PM

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?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to sas_kms

08-07-2014 10:12 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to snoopy369

08-07-2014 10:19 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to sas_kms

08-07-2014 10:22 PM

Well, I think (of x

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to snoopy369

08-07-2014 10:46 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to sas_kms

08-08-2014 07:02 AM

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to sas_kms

08-08-2014 06:50 AM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to sas_kms

08-08-2014 08:49 AM

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?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-08-2014 12:13 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to sas_kms

08-08-2014 12:32 PM

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to stat_sas

08-08-2014 01:30 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to sas_kms

08-08-2014 02:20 PM

Not sure, can you please share generated output?