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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

 

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;


 

View solution in original post

2 REPLIES 2
ballardw
Super User

 

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;


 

SAS4FUN1
Calcite | Level 5

Solved!

Thank you, this solution worked. 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2 replies
  • 611 views
  • 1 like
  • 2 in conversation