Passing a non-temporary array to a proc fcmp function

  • Follow


Hi there,

I just learned about creating functions using proc fcmp that can be called
in the data step.  I would like to pass a non-temporary array to a function,
but I do not think that this is allowed.  Perhaps there is another way in
SAS to do this, or there is a workaround.  Does anyone have any suggestions
for me?

An example I've been playing with is below.  I would like to take answers
from a questionnaire and calculate the average score if a certain number of
answers are provided.  (i.e. if there are 5 questions and 2 are missing,
take the average, but if 3+ are missing, do not score the question).

Thank you very much for your help.

Kim

-----------------------------------------------------------------

proc fcmp outlib = sasuser.funcs.jny;
    function calc_se_scores(array_name[*],max_missing_allowed);
        missing=0;
        do i =1 to dim(array_name);
            if array_name[i] = . then missing=missing +1;
        end;
        if missing <=max_missing_allowed then score=mean(of array_name{*});
/*Error because array_name is not a temporary array*/
        return (score);
endsub;
options cmplib=sasuser.funcs;

data j.data;
   set j.data;

        array md{1:5} md_1 md_2 md_3 md_4 md_5;
        array oh{1:4} oh_1 oh_2 oh_3 oh_4;

        md_score=calc_se_scores(md,2);  /*Error because non-temporary
array*/
        oh_score=calc_se_scores(oh,1);

    run;
0
Reply kim.fernandes (3) 1/6/2010 10:19:56 PM

Bom-dia, como vai voc�?

Why don�t you use the NMISS function?

data questionaire;
input id $8. md_1 md_2 md_3 md_4 md_5;
cards;
Zeca    1  .   .  3.1  .
Joao    3  2.5 3   3  3.5
Dulce   2   .  2   1   1
;
run;

data wanted;
set questionaire;
if nmiss(of md_:) <=2 then Averagescore=mean(of md_:);
run;

Espero-o � �til.

Daniel Fernandez.
Barcelona


2010/1/6 Kim Fernandes <kim.fernandes@gmail.com>:
> Hi there,
>
> I just learned about creating functions using proc fcmp that can be called
> in the data step.  I would like to pass a non-temporary array to a function,
> but I do not think that this is allowed.  Perhaps there is another way in
> SAS to do this, or there is a workaround.  Does anyone have any suggestions
> for me?
>
> An example I've been playing with is below.  I would like to take answers
> from a questionnaire and calculate the average score if a certain number of
> answers are provided.  (i.e. if there are 5 questions and 2 are missing,
> take the average, but if 3+ are missing, do not score the question).
>
> Thank you very much for your help.
>
> Kim
>
> -----------------------------------------------------------------
>
> proc fcmp outlib = sasuser.funcs.jny;
>    function calc_se_scores(array_name[*],max_missing_allowed);
>        missing=0;
>        do i =1 to dim(array_name);
>            if array_name[i] = . then missing=missing +1;
>        end;
>        if missing <=max_missing_allowed then score=mean(of array_name{*});
> /*Error because array_name is not a temporary array*/
>        return (score);
> endsub;
> options cmplib=sasuser.funcs;
>
> data j.data;
>   set j.data;
>
>        array md{1:5} md_1 md_2 md_3 md_4 md_5;
>        array oh{1:4} oh_1 oh_2 oh_3 oh_4;
>
>        md_score=calc_se_scores(md,2);  /*Error because non-temporary
> array*/
>        oh_score=calc_se_scores(oh,1);
>
>    run;
>
0
Reply fdezdan (222) 1/7/2010 9:50:44 AM


Kim,

You have received good advices.

As far as I know, the FCMP function works only with _temporary_ array. You
need to copy PDV vraiables to a temporary array before passing to FCMP
function. See the Example 3: Executing Proc STANDARDIZE on Each Row of a
Data Set in SAS Docs.

Another alternative way is to use read_array() function available in FCMP.
This is used to pass the entire data set and use it as a matrix of rows and
columns to manipulate the elements. After the processing, the resulting
matrix can be accessed as another data set by using write_array() function.

Muthia Kachirayan




On Wed, Jan 6, 2010 at 6:19 PM, Kim Fernandes <kim.fernandes@gmail.com>wrote:

> Hi there,
>
> I just learned about creating functions using proc fcmp that can be called
> in the data step.  I would like to pass a non-temporary array to a
> function,
> but I do not think that this is allowed.  Perhaps there is another way in
> SAS to do this, or there is a workaround.  Does anyone have any suggestions
> for me?
>
> An example I've been playing with is below.  I would like to take answers
> from a questionnaire and calculate the average score if a certain number of
> answers are provided.  (i.e. if there are 5 questions and 2 are missing,
> take the average, but if 3+ are missing, do not score the question).
>
> Thank you very much for your help.
>
> Kim
>
> -----------------------------------------------------------------
>
> proc fcmp outlib = sasuser.funcs.jny;
>    function calc_se_scores(array_name[*],max_missing_allowed);
>        missing=0;
>        do i =1 to dim(array_name);
>            if array_name[i] = . then missing=missing +1;
>        end;
>        if missing <=max_missing_allowed then score=mean(of array_name{*});
> /*Error because array_name is not a temporary array*/
>        return (score);
> endsub;
> options cmplib=sasuser.funcs;
>
> data j.data;
>   set j.data;
>
>        array md{1:5} md_1 md_2 md_3 md_4 md_5;
>        array oh{1:4} oh_1 oh_2 oh_3 oh_4;
>
>        md_score=calc_se_scores(md,2);  /*Error because non-temporary
> array*/
>        oh_score=calc_se_scores(oh,1);
>
>    run;
>
0
Reply muthia.kachirayan (702) 1/7/2010 5:03:19 PM

2 Replies
390 Views

(page loaded in 0.073 seconds)

Similiar Articles:







7/23/2012 6:58:55 PM


Reply: