DATA Step, Macro, Functions and more

Left and right characters of a value with leading zeros

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 120
Accepted Solution

Left and right characters of a value with leading zeros

Hi ,

I have a variable called xy =  000499 19

 

and i want to create var1 = 000499 and var2 = 19

 

 

can anyone pls let me know how to achieve


Accepted Solutions
Solution
‎03-24-2016 10:17 AM
Super Contributor
Posts: 305

Re: Left and right characters of a value with leading zeros

Hello,

 

data want;
xy='000499 19';
var1=scan(xy,1);
var2=scan(xy,2);
run;

 

 

View solution in original post


All Replies
Solution
‎03-24-2016 10:17 AM
Super Contributor
Posts: 305

Re: Left and right characters of a value with leading zeros

Hello,

 

data want;
xy='000499 19';
var1=scan(xy,1);
var2=scan(xy,2);
run;

 

 

Frequent Contributor
Posts: 120

Re: Left and right characters of a value with leading zeros

hi ,

 we have to extract first 6 characters for var1 and next 2 characters for var2

 

i have used

LEFT(xx,6) as Var1,

RIGHT(LEFT(xx,9),2) as Var2,

 

but it didnt work i dont know y ?

Super Contributor
Posts: 305

Re: Left and right characters of a value with leading zeros

data want;
xy='000499 19';
var1=substr(xy,1,6);
var2=substr(xy,8,2);
run;

Frequent Contributor
Posts: 120

Re: Left and right characters of a value with leading zeros

Hi ,

I got it but how is that similar to the left and right function which i have sent you can u pls explain because i have many similar cases where i have to use it

Super Contributor
Posts: 305

Re: Left and right characters of a value with leading zeros

Hello,

 

Left and Right are used in SAS just to align a character string. Probably you took them from excel where indeed they are used to extract from a string.

Frequent Contributor
Posts: 120

Re: Left and right characters of a value with leading zeros

Hi ,

Thank you . But can i make left and right function work in SAS to achieve what i need because i have 10 codes to change and also its not same case with everything for example when we have a condition like

 

xx= NA630011 what is a similar function achieved by below code

 

LEFT(xx) as Var1,

RIGHT(LEFT(xx),1) as Var2,

RIGHT(LEFT(xx),3) as Var3,

RIGHT(LEFT(xx),3) as Var4,

 

 

Thanks

 

 

 

Super User
Posts: 10,552

Re: Left and right characters of a value with leading zeros


chennupriya wrote:

xx= NA630011 what is a similar function achieved by below code

 

LEFT(xx) as Var1,

RIGHT(LEFT(xx),1) as Var2,

RIGHT(LEFT(xx),3) as Var3,

RIGHT(LEFT(xx),3) as Var4,

 

 


 

What would you expect the values of Var1, Var2, Var3 and Var4 to be? I'm not going to guess where you got the example syntax from.

Note: if any of these are single values of 0 or 1 you better point out which 0 and which 1 from the original string contributed it as there would be different ways to get those.

Super User
Posts: 5,099

Re: Left and right characters of a value with leading zeros

The issue you are having is that you never read the SAS documentation for the LEFT and RIGHT functions.  In some other languages, you may be permitted two arguments to these functions.  But in SAS, only one argument is permitted.  You'll need to read the SAS documentation.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 363 views
  • 0 likes
  • 4 in conversation