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

Hi,

I defined a macrovariable:

%let InDomainWhere=>"30DEC2021"D;

and I need in a %if condition to count how many characters are present, how can I do that?

I was currently using

%if %eval(%sysfunc(count(%cmpress(&InDomainWhere.),%str( )))+0) = 0 %then %do;

but I realized that it only counts spaces and not all characters.

How can I do this?

 

Thanks

Luca

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Count how many characters are present: use the %length() macro function.

 

%if %length(&indomainwhere)=0 %then %do;

 

or if necessary

 

%if %length(%cmpres(&indomainwhere)) %then %do;

 

but if &indomainwhere is a date, I don't see why you would need %cmpres

--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

Count how many characters are present: use the %length() macro function.

 

%if %length(&indomainwhere)=0 %then %do;

 

or if necessary

 

%if %length(%cmpres(&indomainwhere)) %then %do;

 

but if &indomainwhere is a date, I don't see why you would need %cmpres

--
Paige Miller
Tom
Super User Tom
Super User

You can use the %LENGTH() function to count the number of BYTES, which in a single byte encoding is the same thing.

If the string in the macro variable could contain UTF-8 characters then you will need to use the KLENGTH() function to count the number of CHARACTERS.

994   %let InDomainWhere=>"30DEC2021"D;
995   %let bytes=%length(&indomainwhere);
996   %let chars=%sysfunc(klength(%superq(indomainwhere)));
997   %put &=bytes &=chars;
BYTES=13 CHARS=13

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

Register Now

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
  • 879 views
  • 0 likes
  • 3 in conversation