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

 

Hello ,

I have a large dataset where I  need to add leading number to some of the variables. here is a subset of the data

Data  have;

    11-000-0067
5511-000-00267
5511-000-00367
    11-000-01166
5511-000-11166


I want to add  55 to those that are missing the leading number to get the following result

 

data want;

5511-000-00671
5511-000-00267
5511-000-00367
5511-000-01166
5511-000-11166

 

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
SuryaKiran
Meteorite | Level 14

Did you miss "1" in your first records for the data you have?

 

data have;
input num $15.;
datalines;
11-000-00671
5511-000-00267
5511-000-00367
11-000-01166
5511-000-11166
;
run;
data want;
set have;
if length(num)=12 then num=cats("55",num);
run;
Thanks,
Suryakiran

View solution in original post

8 REPLIES 8
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Is it always 55 in front, and 1 at end?  I would do something like:

data want;
  set have;
  length want want1 want2 want3 $50;
  if lengthn(scan(have,1,"-")) ne 4 then want1=cats(repeat("5",4-lengthn(scan(have,1,"-")),scan(have,1,"-"));
  else want1=scan(have,1,"-");
  ...
  want=catx('-',want1--want3);
run;

Ie break the string up into three, check the length, padd as needed, then cat together.

Rigler
Calcite | Level 5

I forgot to mention that some values can be

11-000-abcd1 , so this is not a numeric variable.

Thanks.

SuryaKiran
Meteorite | Level 14

Did you miss "1" in your first records for the data you have?

 

data have;
input num $15.;
datalines;
11-000-00671
5511-000-00267
5511-000-00367
11-000-01166
5511-000-11166
;
run;
data want;
set have;
if length(num)=12 then num=cats("55",num);
run;
Thanks,
Suryakiran
Rigler
Calcite | Level 5

this code does not seem to work for me. Please note this is a character variable,

Thanks.

SuryaKiran
Meteorite | Level 14

Please show what you get in output.

Check if leading or trailing blanks are causing the issue.

 

if length(num)=12 then num=cats("55",strip(num));

 

Thanks,
Suryakiran
Rigler
Calcite | Level 5

After another try, this works perfectly. 

Thank you so much!

amatsu
Obsidian | Level 7
data have;
input number:$20.;
cards;
11-000-00671
5511-000-00267
5511-000-00367
11-000-01166
5511-000-11166
;

data want;
  set have;
  length number2 $20.;
  number2 = tranwrd(put(number, 14.-R),' ','5'); 
run;
Astounding
PROC Star

Assuming that you have a character variable that might contain exactly two leading blanks:

 

if (var > ' ') and (var =: '  ') then substr(var, 1, 2) = '55';

 

The assumptions about what is in the data are critical, however, and this only works if the assumptions are correct.

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
  • 2555 views
  • 1 like
  • 5 in conversation