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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Loko
Barite | Level 11

Hello,

 

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

 

 

View solution in original post

8 REPLIES 8
Loko
Barite | Level 11

Hello,

 

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

 

 

chennupriya
Quartz | Level 8

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 ?

Loko
Barite | Level 11

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

chennupriya
Quartz | Level 8

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

Loko
Barite | Level 11

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.

chennupriya
Quartz | Level 8

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

 

 

 

ballardw
Super User

@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.

Astounding
PROC Star

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 8 replies
  • 1131 views
  • 0 likes
  • 4 in conversation