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?
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.
@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.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.