SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Phil_NZ
Barite | Level 11

Hi all SAS Users,

 

I have want to create a variable voi_and_acc=1 if  GEOGN match one of some specific words

For example, I have some input specific words like: Argentina, AuStralia

GEOGN is ARGENTINA and AUSTRALIA.

 

So, I guess my code should be 

data add_sub;
	set work.merge_treat_con;
	 if GEOGN in upcase("Australia","Austria") then voi_and_acc=1;
run;

 

However, the error showing is that

28         data add_sub;
29         	set work.merge_treat_con;
30         	 if GEOGN in upcase("Australia","Austria") then voi_and_acc=1;
                               _
                               22
                               76
ERROR: The right-hand operand must be an array name or a constant value list.  The specified name upcase, is not an array.
ERROR 22-322: Syntax error, expecting one of the following: IN, NOTIN.  

ERROR 76-322: Syntax error, statement will be ignored.

Could you please help me to sort it out?

 

Many thanks.

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@Phil_NZ wrote:

Hi all SAS Users,

 

I have want to create a variable voi_and_acc=1 if  GEOGN match one of some specific words

For example, I have some input specific words like: Argentina, AuStralia

GEOGN is ARGENTINA and AUSTRALIA.

 

So, I guess my code should be 

data add_sub;
	set work.merge_treat_con;
	 if GEOGN in upcase("Australia","Austria") then voi_and_acc=1;
run;

You could also use the FIND function, which allows case insensitive testing of character strings using the 'i' option.

 

if find(geogn,'Australia','i')>0 or find(geogn,'Austria','i')>0 then ...

Adding on to @Kurt_Bremser 's answer, most SAS interfaces have keyboard shortcuts that allow you to change text that you have selected and convert it to uppercase, magically, right before your eyes! On my Windows 10 computer running Base SAS 9.4, the keyboard shortcut is Ctrl-Shift-U. So if you typed mixed case "Australia" and then select it and Ctrl-Shift-U, now you have "AUSTRALIA".

--
Paige Miller

View solution in original post

9 REPLIES 9
Kurt_Bremser
Super User

These are literals, so you do not need a function, just write them in uppercase:

 if GEOGN in ("AUSTRALIA","AUSTRIA")

And you have to use the list immediately following the IN operator; also, the items in the list MUST be literals (constants).

Phil_NZ
Barite | Level 11

Hi @Kurt_Bremser 

 


@Kurt_Bremser wrote:
And you have to use the list immediately following the IN operator

Does it mean that I cannot use 

if GEOGN in upcase() or any function but just only if GEOGN in() ?

 

Warm regards.

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
PaigeMiller
Diamond | Level 26

You can do this:

 

if geogn in (upcase('Australia'))

but it would be less typing to do this:

 

if geogn in ('AUSTRALIA')
--
Paige Miller
ChrisNZ
Tourmaline | Level 20

The syntax I'd use would be:

if GEOGN in %upcase(('Australia','Austria')) 

 This ensures the interpreted string is static, for better performance.

ChrisNZ
Tourmaline | Level 20

Another option is:

if prxmatch('/Australia|Austria/i',GEOGN) 

The list of words can easily be expanded, and the final i make the match case-insensitive. 

PaigeMiller
Diamond | Level 26

@ChrisNZ wrote:

The syntax I'd use would be:

if GEOGN in %upcase(('Australia','Austria')) 

 This ensures the interpreted string is static, for better performance.


It's still less typing to put the country names in uppercase to begin with.

--
Paige Miller
ChrisNZ
Tourmaline | Level 20

I understand the words come as is.    GEOGN match one of some specific words

 

Yes these words could be uppercased too if that doesn't have any negative effect somewhere else

Kurt_Bremser
Super User

@Phil_NZ wrote:

Hi @Kurt_Bremser 

 


@Kurt_Bremser wrote:
And you have to use the list immediately following the IN operator

Does it mean that I cannot use 

if GEOGN in upcase() or any function but just only if GEOGN in() ?

 

Warm regards.


Absolutely. See the documentation of the IN Operator.
The IN is set up by the data step or SQL compiler at compile time, so it cannot have variable expressions or functions in it.

 

PaigeMiller
Diamond | Level 26

@Phil_NZ wrote:

Hi all SAS Users,

 

I have want to create a variable voi_and_acc=1 if  GEOGN match one of some specific words

For example, I have some input specific words like: Argentina, AuStralia

GEOGN is ARGENTINA and AUSTRALIA.

 

So, I guess my code should be 

data add_sub;
	set work.merge_treat_con;
	 if GEOGN in upcase("Australia","Austria") then voi_and_acc=1;
run;

You could also use the FIND function, which allows case insensitive testing of character strings using the 'i' option.

 

if find(geogn,'Australia','i')>0 or find(geogn,'Austria','i')>0 then ...

Adding on to @Kurt_Bremser 's answer, most SAS interfaces have keyboard shortcuts that allow you to change text that you have selected and convert it to uppercase, magically, right before your eyes! On my Windows 10 computer running Base SAS 9.4, the keyboard shortcut is Ctrl-Shift-U. So if you typed mixed case "Australia" and then select it and Ctrl-Shift-U, now you have "AUSTRALIA".

--
Paige Miller

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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
  • 9 replies
  • 2562 views
  • 8 likes
  • 4 in conversation