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

Hello,

 

If the input variable contains a comma, then the containscomma variable should be set to 1: 

 

data _null;
	input = "Why, oh why";
	if findw(input, ",") > 0 then do; containscomma=1; end;
	else containscomma=0;
	put containscomma;
run;

It does contains a comma, yet containscomma is set to 0.

 

Can someone please advise on how I can make the code work as intended?

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Findw is a function for finding a word delimited by certain characters.  Comma is not a word, it is a delimiter.  If you want to find text in another you can use index() - these are all well described in the manual:

data _null;
	input = "Why, oh why";
	containscomma=ifn(index(input,","),1,0);
	put containscomma;
run;

Note I use the binary choise ifn() function to simplfy the code.  Also, I do not put an operator after the index, basically if index finds the character then the result is a positive number, hence if positive number=true return first, else second. 

View solution in original post

1 REPLY 1
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Findw is a function for finding a word delimited by certain characters.  Comma is not a word, it is a delimiter.  If you want to find text in another you can use index() - these are all well described in the manual:

data _null;
	input = "Why, oh why";
	containscomma=ifn(index(input,","),1,0);
	put containscomma;
run;

Note I use the binary choise ifn() function to simplfy the code.  Also, I do not put an operator after the index, basically if index finds the character then the result is a positive number, hence if positive number=true return first, else second. 

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
  • 1 reply
  • 1939 views
  • 2 likes
  • 2 in conversation