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
Rhodochrosite | Level 12
Can you try
where (low_soc_code/1000000) < 1 AND (upper_soc_code/1000000) < 1

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 983 views
  • 5 likes
  • 4 in conversation