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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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