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

Hi,

 

When you use for instance the SUBSTR function to create a new variable, the length of that new variable is equal to the length of the variable you use as an argument.

 

I created a character function with PROC FCMP and I'd like my function to have the same property.

 

If my function statement is like this :

function extrait(chaine $,dlm $,pos) $ ;

The length of the variable created when I use this function is 33 (BTW, why?). I can give an arbitrary large value for the length of the created variables (say 500...) but this would be a waste of resources.

 

I'd like that length to be equal to the length of the variable I'll use as the "chaine" argument.

 

is this possible?

 

best regards

 

Sébastien

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@SR_FR wrote:

Hi,

 

When you use for instance the SUBSTR function to create a new variable, the length of that new variable is equal to the length of the variable you use as an argument.

 

I created a character function with PROC FCMP and I'd like my function to have the same property.

 

If my function statement is like this :

function extrait(chaine $,dlm $,pos) $ ;

The length of the variable created when I use this function is 33 (BTW, why?). I can give an arbitrary large value for the length of the created variables (say 500...) but this would be a waste of resources.

 

I'd like that length to be equal to the length of the variable I'll use as the "chaine" argument.

 

is this possible?

 

best regards

 

Sébastien


Likely the reason your test variable has length of 33 is the first returned value had that length. The basic rule for non-defined lengths of character variables is the first use will set the length.

 

The easiest I could see if you don't want to be bothered to assign an explicit length to a "new" variable is to do something such as call to substr or similar function prior to the function call:

 

dummy = substr(var,1);

dummy = extrait(var, ',',3) ; (or however you want to call your function.

View solution in original post

2 REPLIES 2
ballardw
Super User

@SR_FR wrote:

Hi,

 

When you use for instance the SUBSTR function to create a new variable, the length of that new variable is equal to the length of the variable you use as an argument.

 

I created a character function with PROC FCMP and I'd like my function to have the same property.

 

If my function statement is like this :

function extrait(chaine $,dlm $,pos) $ ;

The length of the variable created when I use this function is 33 (BTW, why?). I can give an arbitrary large value for the length of the created variables (say 500...) but this would be a waste of resources.

 

I'd like that length to be equal to the length of the variable I'll use as the "chaine" argument.

 

is this possible?

 

best regards

 

Sébastien


Likely the reason your test variable has length of 33 is the first returned value had that length. The basic rule for non-defined lengths of character variables is the first use will set the length.

 

The easiest I could see if you don't want to be bothered to assign an explicit length to a "new" variable is to do something such as call to substr or similar function prior to the function call:

 

dummy = substr(var,1);

dummy = extrait(var, ',',3) ; (or however you want to call your function.

SR_FR
Obsidian | Level 7

the solution your propose does the job !

 

thanks!

 

about the 33, well, it's still a mystery...

 

The first returned value can not help to understand this length since the length of a character variable is decided during the compilation of the program.

 

may be the complete program would help to solve the mystery...

 

proc fcmp outlib=sasuser.funcs.temp;
   function extrait(chaine $,dlm $,pos) $ ;
   temp=0;
      do i=1 to pos;
         temp=find(chaine,dlm,temp+1);
      end;
   return(substr(chaine,1,temp-1));
   endsub;
run;

options cmplib=sasuser.funcs;

data toto;
   x="azz,bs,cezz,dd,e";
   y=3;
   dlm=',';
   gogo=extrait(x,dlm,y);
run;

Sébastien

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 980 views
  • 0 likes
  • 2 in conversation