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

Hello

My first post. I am using SAS 9.4 in Windows. I am importing data from an Excel file. The data is alpha-numeric. The variables range in length from 2 to 7 characters. For example:

151

534

345697

1A875

The variables need to be 7 digits with leading zeroes. Assuming the variable I am importing is names 'variable1', the code i am using is:

 

proc import datafile="xxxxxxx.xlsx"
out=dataset1
dbms=xlsx ;
run;

data dataset2;
set dataset1;
format variable2 $7.0 ;
variable2 = translate(right(variable1),"0"," ");

run;

 

The variables in the output set are 7 digits but it leaves off the last character. Using the above data set, the output is:

0000015

0000053

0034569

0001A87

 

I just can't get the right output and I know it worked last month!! Aiiiiii

Any advice?

thanks

 

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

The problem is probably that the VARIABLE1 variable has a length of 8. So when you right-align it, you get one more leading zeros than you want, and the last character is cut off when assigning the value to VARIABLE2 which has a length of 7.

 

The easiest way out is probably just to drop the first leading zero:

variable2 = substr(translate(right(variable1),"0"," "),2);

View solution in original post

2 REPLIES 2
s_lassen
Meteorite | Level 14

The problem is probably that the VARIABLE1 variable has a length of 8. So when you right-align it, you get one more leading zeros than you want, and the last character is cut off when assigning the value to VARIABLE2 which has a length of 7.

 

The easiest way out is probably just to drop the first leading zero:

variable2 = substr(translate(right(variable1),"0"," "),2);
Mofro13
Calcite | Level 5
That did it. Thanks for the help!!

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2 replies
  • 576 views
  • 0 likes
  • 2 in conversation