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

I know there is a DS2 thread, but I don't know how to use it in fcmp function-package, the documentation only has a multithreaded case and there is too little explanation

/*this is fcmp function*/
PROC FCMP OUTLIB=WORK.FUNCS.MATH;
FUNCTION GAUSS_DECAY(DISTANCE,SIGMA);
	WEIGHT=1*EXP(-(DISTANCE-0)**2/(2*SIGMA**2));
		IF WEIGHT=1 THEN WEIGHT=0;
RETURN(WEIGHT);
	ENDSUB;
RUN;
QUIT;
/*Convert fcmp function package to ds2 function package*/
PROC DS2 ;
PACKAGE FUNCTION_PACK / OVERWRITE=YES LANGUAGE='FCMP' TABLE='WORK.FUNCS';
ENDPACKAGE;
RUN;
QUIT;
/*THIS IS DS2 DATA SETP*/
PROC DS2;
THREAD T / OVERWRITE=YES;
METHOD RUN();
END;
NEDTHREAD;
RUN;
DATA SAMPLE / OVERWRITE=YES; DCL DOUBLE X HAVING LABEL '循环次数'; DCL DOUBLE WEIGHT_MEAN HAVING LABEL 'SIGMA'; DCL DOUBLE WEIGHT_MAX HAVING LABEL 'SIGMA'; DCL DOUBLE WEIGHT_MIN HAVING LABEL 'SIGMA' ; DCL PACKAGE FUNCTION_PACK P();
DCL THREAD T T1; METHOD INIT();
SET FROM T1 THREADS=8; DO X =10 TO 0 BY -0.0001; WEIGHT_MEAN= P.GAUSS_GRAPHIC(X,1.3696199923); WEIGHT_MAX=P.GAUSS_GRAPHIC(X,1.9840148); WEIGHT_MIN=P.GAUSS_GRAPHIC(X,0.8215555); OUTPUT; END; STOP; END; ENDDATA; RUN; QUIT;
1 ACCEPTED SOLUTION

Accepted Solutions
AhmedAl_Attar
Ammonite | Level 13

Hi @_Sas_Beginner_ 

I think you have miss understood the threads concept in SAS Proc DS2!

DS2 threads are not your typical programming language threads!

 

DS2 Threads are stored program that can be executed in parallel. They must contains at least one explicit method (INIT, RUN, TERM), can declare local and global variables that affect the PDV, and can instantiate and use packages.

 

Typically,

  • Native provided functions, and custom developed function operate on a single/multiple variable(s) within a record/observation of a data set/table.
  • Procedures that operate on an entire table/data set can be threaded behind the scene in order to split the processing of the entire table across multiple threads/smaller processing units

So to translate this into Proc DS2, check this slightly modified example from Reasons to love PROC DS2

/* Create Sample Data */
data test; 
do x=1 to 1231600;
 j=x*x; 
output; 
end; 
run; 
 
proc ds2; 
  /* Create a thread to manipulate the records within the data set */
  /* Note: You would call Native/Custom functions here */
  thread newton/overwrite=yes; 
    dcl double y count; 
    dcl bigint thisThread;
    drop count; 
    method run(); 
      set test; 
      y=(x*j*x)/(j+x); 
      thisThread=_threadid_; 
      count+1;
    end; 
    method term();
      put '**Thread' _threadid_ 'processed'  count 'rows:';
    end;
  endthread; 
  run; 
quit; 
 
proc ds2; 
  data _null_; 
    dcl thread newton frac; 
    method run(); 
         /*  Run the processing in Parallel across 4 threads */
         set from frac threads=4; 
    end; 
  enddata;
  run; 
quit;

For additional Proc DS2 examples try these two links

Hope this helps

View solution in original post

7 REPLIES 7
_Sas_Beginner_
Quartz | Level 8
How do I define a thread in the fcmp function package in proc ds2?
PaigeMiller
Diamond | Level 26

Please shorten your subject line, the subject of your question is not "Good morning SAS experts, I have a programming question, How do I multi-thread this DS2 process". The subject of your question is "How do I multi-thread this DS2 process". This shorter subject line will attract the proper people knowledgeable in DS2.

--
Paige Miller
_Sas_Beginner_
Quartz | Level 8
Thank you for reminding me
AhmedAl_Attar
Ammonite | Level 13

Hi @_Sas_Beginner_ 

I think you have miss understood the threads concept in SAS Proc DS2!

DS2 threads are not your typical programming language threads!

 

DS2 Threads are stored program that can be executed in parallel. They must contains at least one explicit method (INIT, RUN, TERM), can declare local and global variables that affect the PDV, and can instantiate and use packages.

 

Typically,

  • Native provided functions, and custom developed function operate on a single/multiple variable(s) within a record/observation of a data set/table.
  • Procedures that operate on an entire table/data set can be threaded behind the scene in order to split the processing of the entire table across multiple threads/smaller processing units

So to translate this into Proc DS2, check this slightly modified example from Reasons to love PROC DS2

/* Create Sample Data */
data test; 
do x=1 to 1231600;
 j=x*x; 
output; 
end; 
run; 
 
proc ds2; 
  /* Create a thread to manipulate the records within the data set */
  /* Note: You would call Native/Custom functions here */
  thread newton/overwrite=yes; 
    dcl double y count; 
    dcl bigint thisThread;
    drop count; 
    method run(); 
      set test; 
      y=(x*j*x)/(j+x); 
      thisThread=_threadid_; 
      count+1;
    end; 
    method term();
      put '**Thread' _threadid_ 'processed'  count 'rows:';
    end;
  endthread; 
  run; 
quit; 
 
proc ds2; 
  data _null_; 
    dcl thread newton frac; 
    method run(); 
         /*  Run the processing in Parallel across 4 threads */
         set from frac threads=4; 
    end; 
  enddata;
  run; 
quit;

For additional Proc DS2 examples try these two links

Hope this helps

AhmedAl_Attar
Ammonite | Level 13

PS. The DS2 Threads definition paragraph is quoted from Mark Jordan's (@SASJedi) "Mastering the SAS DS2 Procedure" book

_Sas_Beginner_
Quartz | Level 8
It's a little expensive, but you recommended it, and I ordered it,thanks!
_Sas_Beginner_
Quartz | Level 8
thanks for your reply!I understand that the general DS2 thread is the process of calculating data against an existing data set, not the process of generating the data set

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 971 views
  • 1 like
  • 3 in conversation