BookmarkSubscribeRSS Feed
Stacy1
Calcite | Level 5

I am trying to create a z score function so that it can be used repeatedly anytime I want to calculate the z score.

I know how to calculate the z score, the only problem I have is to make it a function that can be reused.

I am using the cars data set which I will attach to this question.

First I created a library named ztest and imported the dataset;

libname ztest "\Users\Ifeoma\Downloads\newsas";
proc import datafile = "C:\Users\Ifeoma\Downloads\cars.xlsx"
out = ztest.zscore
dbms = xlsx replace;
getnames = yes;
run;

Here is my code for calculating z score which worked.

                    data test(keep = length horsepower mpg_city);
                           set ztest.zscore;
                    run;

                    proc standard data = test mean = 0 std = 1 out = z_test;
                            var length horsepower mpg_city;
                    run;

The data set WORK.Z_TEST has 421 observations and 3 variables.
NOTE: PROCEDURE STANDARD used (Total process time):
real-time 0.01 seconds
CPU time 0.01 seconds

 

 

Here is my code to create a z score function which did not work;


proc fcmp outlib = ztest.test;
function get_z(length, horsepower , mpg_city);
proc standard data = test mean = 0 std = 1 out = z_test;
var length horsepower mpg_city;
run;
options cmplib=ztest.test;
score = get_z(length, horsepower , mpg_city);
put score = ;

Here is the error I got;

NOTE: No CMP or C functions found in library ztest.test.
70 function get_z(length, horsepower , mpg_city);

ERROR: Subroutine 'get_z' was not terminated with ENDSUB.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE FCMP used (Total process time):
real-time 0.07 seconds
CPU time 0.04 seconds

 

Is there a way to correct this procedure or is there another procedure to use? 

 

 

3 REPLIES 3
Reeza
Super User
The way you're currently designing this will not work. You'll need to provide some more requirements about what you're trying to accomplish before we can offer a solution.

You may need a macro instead of a function for example. Though PROC STANDARD does that anyways so not sure what value you'd get otherwise.

You will not be able to call it in the method you've shown as well, so more specifications are needed.

ChrisNZ
Tourmaline | Level 20

You are attempting to create a "function" that calls a procedure which outputs a data set.

But somehow you expect to get a value when you call this function.

A few steps are missing it seems.

 

 

PaigeMiller
Diamond | Level 26

@Stacy1 wrote:

I am trying to create a z score function so that it can be used repeatedly anytime I want to calculate the z score.

I know how to calculate the z score, the only problem I have is to make it a function that can be reused.


proc fcmp outlib = ztest.test;
function get_z(length, horsepower , mpg_city);
proc standard data = test mean = 0 std = 1 out = z_test;
var length horsepower mpg_city;
run;


I am not aware that functions in PROC FCMP can include SAS PROC statements.

 

What is wrong with simply using PROC STDIZE to obtain both the original variables and the zscore in a single data set? I don't see a need for a function/subroutine/macro to do this.

--
Paige Miller

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 834 views
  • 0 likes
  • 4 in conversation