Studying for the SAS Advanced Cert exam and came across this problem in a practice exam. I do not understand why my PROC FCMP function does not return the same results as the same code in the data step. It only returns the first letter of the second word.
data testscandata;
infile cards dsd dlm='|';
input one $ two $ one_two $;
datalines;
one|two|one two
one|two|one two
one|two|one two
one|two|one two
;
run;
PROC FCMP OUTLIB=WORK.FUNCTIONS.DEV;
FUNCTION scantx (STRING $) $ 100;
SCNOUT=catx(" ",scan(STRING,2), scan(STRING,1));
RETURN (SCNOUT);
ENDSUB;
RUN;
OPTIONS CMPLIB=WORK.FUNCTIONS;
DATA TEST_FUNC;
SET WORK.testscandata;
two_one_func = scantx(one_two);
two_one_test = catx(" ",scan(one_two,2), scan(one_two,1));
RUN;
Consider what the LENGTH you may have defined for the variable SCNOUT in the function might be.
See what happens with
PROC FCMP OUTLIB=WORK.FUNCTIONS.DEV; FUNCTION scantx (STRING $) $ 100; length scnout $ 100; SCNOUT=catx(" ",scan(STRING,2), scan(STRING,1)); RETURN (SCNOUT); ENDSUB; RUN;
@SAS4FUN1 wrote:
Studying for the SAS Advanced Cert exam and came across this problem in a practice exam. I do not understand why my PROC FCMP function does not return the same results as the same code in the data step. It only returns the first letter of the second word.
data testscandata;
infile cards dsd dlm='|';
input one $ two $ one_two $;
datalines;
one|two|one two
one|two|one two
one|two|one two
one|two|one two
;
run;PROC FCMP OUTLIB=WORK.FUNCTIONS.DEV;
FUNCTION scantx (STRING $) $ 100;
SCNOUT=catx(" ",scan(STRING,2), scan(STRING,1));
RETURN (SCNOUT);
ENDSUB;
RUN;OPTIONS CMPLIB=WORK.FUNCTIONS;
DATA TEST_FUNC;
SET WORK.testscandata;
two_one_func = scantx(one_two);
two_one_test = catx(" ",scan(one_two,2), scan(one_two,1));
RUN;
Consider what the LENGTH you may have defined for the variable SCNOUT in the function might be.
See what happens with
PROC FCMP OUTLIB=WORK.FUNCTIONS.DEV; FUNCTION scantx (STRING $) $ 100; length scnout $ 100; SCNOUT=catx(" ",scan(STRING,2), scan(STRING,1)); RETURN (SCNOUT); ENDSUB; RUN;
@SAS4FUN1 wrote:
Studying for the SAS Advanced Cert exam and came across this problem in a practice exam. I do not understand why my PROC FCMP function does not return the same results as the same code in the data step. It only returns the first letter of the second word.
data testscandata;
infile cards dsd dlm='|';
input one $ two $ one_two $;
datalines;
one|two|one two
one|two|one two
one|two|one two
one|two|one two
;
run;PROC FCMP OUTLIB=WORK.FUNCTIONS.DEV;
FUNCTION scantx (STRING $) $ 100;
SCNOUT=catx(" ",scan(STRING,2), scan(STRING,1));
RETURN (SCNOUT);
ENDSUB;
RUN;OPTIONS CMPLIB=WORK.FUNCTIONS;
DATA TEST_FUNC;
SET WORK.testscandata;
two_one_func = scantx(one_two);
two_one_test = catx(" ",scan(one_two,2), scan(one_two,1));
RUN;
Solved!
Thank you, this solution worked.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.