BookmarkSubscribeRSS Feed
haider_imam
Calcite | Level 5

I am trying to categorized data based on what the last two characters are in a variable

 

rand1=rand('uniform');
rand2=rand('uniform'); 

if rand1>0.90 Then do; 
	if (POIDS>0 and substr(USAGE,2,2) IN('20','21')) then do;
		if rand2>0.5 then RadiusSurcharge="A";
		ELSE RadiusSurcharge="B";
		end;
	if (POIDS=0 and substr(USAGE,2,2) IN('20','21')) then do;
		if rand2>0.5 then RadiusSurcharge="C";
		else RadiusSurcharge="D";
		end;
end;
if rand1>0.90 then do;
	if (POIDS>0 and substr(USAGE,2,2) NE('20','21')) then do;
		if rand2>0.5 then RadiusSurcharge="E";
		else RadiusSurcharge="F";
		end;
	if (POIDS=0 and substr(USAGE,2,2) NE('20','21')) then do;
		if rand2<0.333 then RadiusSurcharge="G";
		else if rand2>0.333 and rand2<0.666 then do RadiusSurcharge="H";
		else RadiusSurcharge="I";
		end;
		end;
end;		

The top half, I use IN function so that the obs that does contain '20' or '21' gets assigned other variables accordinly

 

For the second half I am trying to use NE so that the observations that don't contain '20' or '21' but i keep getting the following errors, 

 

     	if (POIDS>0 and substr(USAGE,2,2) NE('20','21')) then do;
                                                     _
                                                     388
                                                     200
ERROR 388-185: Expecting an arithmetic operator.

ERROR 200-322: The symbol is not recognized and will be ignored.

Please advise! 

1 REPLY 1
novinosrin
Tourmaline | Level 20

Correction

 


     	if (POIDS>0 and substr(USAGE,2,2) NOT IN ('20','21')) then do;

 

 

Notes:

1. To filter a list that has more than one operand to evaluate, an equality operator is not appropriate . For an equality operator to work, you would have to work with just one operand 

2. While you coded your IN the list correctly, all you need is NOT IN for all values that are not 20,21 to be true

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