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

Hello

I have a two variables (v1 and v2) of 3 digits each. v2 has a charterer format (per the original dataset).

I'm trying to say if the 3rd digit of var1 equal to the (3rd digit of var2)+1 then set the value of a variable sem to 1.

I tried to use substr for this task but it didn't work. I tried also to create a new variable based on the 3rd digit but it also didn't work.

below is a simplified example of what I'm trying to do.

data mydata;
input v1 v2 $;
datalines;
131 341
214 322
364 112
114 215
213 236
226 114
237 934
;
length sem 3.; if substr(v1, 3, 1)=(substr(v2, 3, 1)+1) then sems=1; s1=substr(v1,3,1); s2=substr(v1,3,1)+1; run;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

You cannot use SUBSTR successfully on a numeric variable, you need to convert the values to character first:

if substr(put(v1,z3.),3,1) = put(input(substr(v2,3,1),1.)+1,1.) then sems=1;

View solution in original post

7 REPLIES 7
Kurt_Bremser
Super User

You cannot use SUBSTR successfully on a numeric variable, you need to convert the values to character first:

if substr(put(v1,z3.),3,1) = put(input(substr(v2,3,1),1.)+1,1.) then sems=1;
AMSAS
SAS Super FREQ

If you treat the variables as numeric then it is simple enough

 

data mydata;
	input v1 v2 ;
	if mod(v1,10)=mod(v2,10)+1 then
		sems=1 ;
	else
		sems=0 ;
datalines;
131 341
214 322
364 112
114 215
213 236
226 114
237 934
995 994
;
run ;

I did notice you didn't have a records where the 3rd digit of v2 + 1 is equal to the 3rd digit of v1 so I added the last data line:
995 994 
3rd digit of v2 is 4 adding 1 gives you 5 which is the 3rd digit of v1

I have questions like what happens when the 3rd digit of v2 is 9?

 

Abdulla1
Quartz | Level 8

Thanks, this is helpful to deal with the numeric data. I didn't know about the mod function before. however, I'll need to convert the char data format into numbers.

and regarding what happen if the last digit was 9. that is a case that cannot happen within my dateset.  the 3rd number represent grade of product which cannot be greater than 7.

PaigeMiller
Diamond | Level 26

I have a two variables (v1 and v2) of 3 digits each. v2 has a charterer format (per the original dataset).

 

Just because you receive the data that way doesn't mean you have to work with the data that way. Create a numeric version of v2, and the rest is easy.

--
Paige Miller
Abdulla1
Quartz | Level 8

Sure Paige.

I just didn't know that substr cannot be applied on numeric data, nor I knew about the mode function before. I still a beginner when it comes to sas coding.

 

 

PaigeMiller
Diamond | Level 26

@Abdulla1 wrote:

Sure Paige.

I just didn't know that substr cannot be applied on numeric data, nor I knew about the mode function before. I still a beginner when it comes to sas coding.

 

 


That's fine, Abdulla1. Just to be 100% clear, the advice to convert data into more usable forms applies generally to all of your current and future SAS work; not just in this one case.

--
Paige Miller
ballardw
Super User

@Abdulla1 wrote:

Sure Paige.

I just didn't know that substr cannot be applied on numeric data, nor I knew about the mode function before. I still a beginner when it comes to sas coding.

 

 


It is not that you cannot apply character functions to numeric variables but that the rules SAS will use to do the implied desire of using a character value from the numeric value will quite often not result in what you intend. Generally, IIRC, SAS will default to using a BEST12. format, which will also right justify values when creating the character version. So the default behavior often leads to multiple leading blanks when the number is an integer using fewer than 12 digits. When the value is a very large number or very small decimal then SAS will use exponential notation to fit into the 12 characters that BEST12 displays.

 

Moral of the story, if you have a numeric variable and want to do some sort of character manipulation then you specify the rules of how the character version is created. Typically that means using the PUT function with a specific format and possibly the -L option to left justify the result so there are no leading blanks.

Here is a brief example that demonstrates several issues around this conversion to character process. Below the data step takes a small number and creates text values 4 ways. The quote function places quote characters around the result of the conversion to show the results. Text4 below is the default conversion.

data example;
   value = 123;
   text1 = quote(put(value,f8.));
   text2 = quote(put(value,f8. -L));
   text3 = quote(strip(put(value,f8. -L)));
   text4 = quote(value);
run;

Likely you would not want quote characters but they do demonstrate behavior. Depending on exactly how you need to use the result of the conversion you may want the Strip function to remove the trailing blanks when used.

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