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

Hello everyone,

I am using survey data and I have two variables low_soc_code and upper_soc_code. Some of the these variables have six digits and some have eight. I want to only see the six digit low_soc_code and upper_soc_code? Any suggestions? I was thinking about using the where or substr function.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

The last two digits of an integer is just the result of taking MOD 100.

if (mod(lower_code_soc,100) ne 0) or  (mod(upper_soc_code,100) ne 0) then delete;

View solution in original post

9 REPLIES 9
ballardw
Super User

Are these variables numeric or character?

 

If the variable is character then you can use LENGTH (or Klength if using a double-byte character language) function to return the number of characters in a string.   So something like : If length(variable) = 6 (and Length(othervariable) =6 if you need both) to be exactly 6 characters. It isn't clear if you want one or both variables to have the length.

 

If numeric you may have to provide examples as approaches will vary whether decimal values might be present.

Jssa8423
Fluorite | Level 6

lower_soc_code=13201101 upper_soc_code=13201101

 

and some have six digits lower_soc_code=13211100 upper_soc_code=13211100

 

Ideally, I'd like to filter and remove any lower_code_soc and upper_soc_code where the last two digits do not equal 0

Tom
Super User Tom
Super User

The last two digits of an integer is just the result of taking MOD 100.

if (mod(lower_code_soc,100) ne 0) or  (mod(upper_soc_code,100) ne 0) then delete;
ballardw
Super User

@Jssa8423 wrote:

lower_soc_code=13201101 upper_soc_code=13201101

 

and some have six digits lower_soc_code=13211100 upper_soc_code=13211100

 

Ideally, I'd like to filter and remove any lower_code_soc and upper_soc_code where the last two digits do not equal 0


So which is it, 6 digits OR last two digits?

And do you want to remove records containing such values (filter) or replace the value with a missing value (or something else)?

And everything you show has 8 digits. So please show examples of each type of start value and what the desired result for the data should be.

 

If you have a numeric value you can get the numeric value of the last 2 digits IF is an integer with

 

last2 = mod(variable,100);

So if Last2 = 0 the last two digits were 00. If you want to replace the value of the variable you could do something like:

IF Mod(variable,100) = 0 then <what you want goes here>. Delete would remove the record. "then variable=. ;"

would set the value to missing.

But that doesn't care about the "length" of the variable.

 

 

Hint for the future: If a variable, especially a code of some sort, that is not going to be used in arithmetic it should be character.

Tom
Super User Tom
Super User

If you entered SS numbers as numbers then you need to watch out for LEADING zeros.

To convert/display the numbers as 9 digit strings with leading zeros use the Z format.

AhmedAl_Attar
Ammonite | Level 13
Can you try
where (low_soc_code/1000000) < 1 AND (upper_soc_code/1000000) < 1

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1333 views
  • 5 likes
  • 4 in conversation