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.
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;
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.
The variables are numeric.
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
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;
@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.
Thank you, I appreciate it.
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.