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

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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