BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

Hello,

I want to print all values from this loop.

It's my code.

I have in result only the end value

Can you help me

Thank you

proc fcmp outlib=work.pack1.finc1;
    function Myfunc1(var);
           
        do i =2 to int(sqrt(var));
        b=i;
        end;
       return (b);
    endsub;
quit;

options cmplib=work.pack1;

data _null_;
result=MyFunc1(100);
put result=;
run;




1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@mansour_ib_sas 

If you just want to write the intermediary values to the SAS log for testing then adding a simple put b=; statement to the function would do the job.

proc fcmp outlib=work.pack1.finc1;
  function Myfunc1(var);
    do i =2 to int(sqrt(var));
      b=i;
      put b=;
    end;
    return (b);
  endsub;
quit;

View solution in original post

6 REPLIES 6
ballardw
Super User

Provide an example of what you actually expect the output to look like.

 

If you expect to get more than one value from a single function call then you have to provide a mechanism to pass the values. Your function is designed to pass in only one value and return only one as written.

 

Look at the documentation for OUTARGS on updating a number of variables and returning the updated values.

KachiM
Rhodochrosite | Level 12

Hi @mansour_ib_sas 

 

A function is used to return ONE value at a time. Your
FCMP function between "do" and "end" cycles all values
between 2 and sqrt(var) and the final value at the end of the loop
as saved in B is returned to the Data Step . So you are getting one value.

 

If you want to have all the values, you rewrite the function
to return one value. Use the Data Step to call the function
for each value of i between 2 to 100. Here is the revised
solution.

 

proc fcmp outlib=work.pack1.finc1;
    function Myfunc1(var);
           
        return(int(sqrt(var)));
         
    endsub;
quit;

options cmplib=work.pack1;

data _null_;
   do i = 2 to 100;
      result=MyFunc1(i);
      put result=;
   end;
run;
KachiM
Rhodochrosite | Level 12

Hi @mansour_ib_sas 

 

Another look at your function based on the suggestion of @ballardw.

 

A function can be made to return more than ONE Value provided you pass a _temporary_ array from the Data Set. The FCMP function can be made to fill the array and at the end pass the filled array to the Data Set. Passing array from the Data Step to the FCMP function which can pass the filled-array back to Data Step can be done using the keyword OUTARGS. Actually this keyword makes Data Step (see Array M[ ] ) to pass the address of M to share in FCMP Environment (See Array K[ ]). Any change in K reflects in M simultaneously. The FCMP function must return a value by definition. Hence, we return a dummy value, 1 in this case, which will be ignored in the Data Step as we are interested in the values of Array.

 

OUTARGS for a FCMP function is undocumented. Usually it is used with SUBROUTINE. Here comes the function:

 

proc fcmp outlib=work.pack1.finc1;
    function Myfunc1(var, k[*]);
    outargs k;
           
        do i = 2 to int(sqrt(var));
         k[i] = i;
        end;
       return (1);
    endsub;
quit;

options cmplib=work.pack1;

data _null_;
   array m[100] _temporary_;
   result=MyFunc1(100, m);
   do i = 1 to dim(m);
       put m[i]=;
   end;
run;

We do not know in advance how many values will be returned. In this case it can't be more than 10 as we take the square-root of 100 in the Function. So we size a _temporary_ array (M[ ]) with 10 in the Data Step.

 

 

Patrick
Opal | Level 21

@mansour_ib_sas 

If you just want to write the intermediary values to the SAS log for testing then adding a simple put b=; statement to the function would do the job.

proc fcmp outlib=work.pack1.finc1;
  function Myfunc1(var);
    do i =2 to int(sqrt(var));
      b=i;
      put b=;
    end;
    return (b);
  endsub;
quit;
hashman
Ammonite | Level 13

@mansour_ib_sas:

Make the function character and return a string; then just print the result in the DATA step. Or parse the result If you want to create/print separate numeric values. For example:

proc fcmp outlib = work.pack1.func1;                                                                                                    
  function myfunc1 (var) $ ;                                                                                                            
    length r $ 32767 ;                                                                                                                  
    do i = 2 to sqrt (var) ;                                                                                                            
      r = catx (" ", r, i) ;                                                                                                            
    end ;                                                                                                                               
    return (r) ;                                                                                                                        
  endsub;                                                                                                                               
quit ;                                                                                                                                  
                                                                                                                                        
options cmplib = work.pack1 ;                                                                                                           
                                                                                                                                        
data _null_;                                                                                                                            
   result = myfunc1 (150) ;                                                                                                             
 * print the whole thing ;                                                                                                              
   put result ;                                                                                                                         
 * create a number and print separately ;                                                                                               
   do _n_ = 1 to countw (result) ;                                                                                                      
     num = input (scan (result, _n_), d.) ;                                                                                             
     put num ;                                                                                                                          
   end ;                                                                                                                                
run;          

Kind regards

Paul D.

mansour_ib_sas
Pyrite | Level 9

hello and thank you for all your responses;

My final objectif in this work is to detects Prime Numbres of others.

The error I made is not to stop the loop when the mod is 0. 

I understood my error by debugging my program with your help.

Thanks again, if you have other suggestions, they are welcome.

proc fcmp outlib=work.pack1.MyFunc1;
    function MyFunc1(var$) $10;
       length var1 $10;
        
        do i =2 to int(sqrt(var));
        if mod(var,i)=0 then do; var1="NbrNPre";leave;end;
        else do; var1="NbrPre";end;
        b= mod(var,i);
       
       put var=;
       put i=;
       put b=;
       
       
        end;
        
       return (var1);
    endsub;
quit;

data test;
input a;
cards;
34679
34678
23456789
; run;

options cmplib=work.pack1;

data test1;
set test;
    result=MyFunc1(a);
run;



 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 6 replies
  • 523 views
  • 1 like
  • 5 in conversation