BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Nietzsche
Lapis Lazuli | Level 10

hi all, 

 

If run the following code, I can create a new column "new_val" that is the sub of the original value, which returns 98.

 

data news;
val='06MAY98';
new_val=substr(val, 6, 2);

proc print data=news; run;
 
 

image.png

 

 

 

This time I want to replace the year 98 with 99, so I wrote this following code, but instead of getting '06MAY99', I got a 0. 

 

data news;
val='06MAY98';
new_val=substr(val, 6, 2) = 99;

proc print data=news; run;

image.png

 

 

is there any way to fix this?

 

 

 

Passed SAS Base Programming (2022 Dec)
Preparing for SAS Advanced Programming (2026)
1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hi @Nietzsche,

 

Your result 0 is the Boolean value saying that the equality 98 = 99 is false. (The character string substr(val, 6, 2)='98' was automatically converted to the number 98, see the corresponding note in the log.)

 

The SUBSTR (left of =) function modifies the value of the variable in the first argument, so you should create NEW_VAL first with the old value and then modify it:

data news;
val='06MAY98';
new_val=val;
substr(new_val, 6)='99';
run;

Alternatively, you could define NEW_VAL like this

new_val=substr(val, 1, 5)||'99';

using the SUBSTR (right of =) function. But then the length of NEW_VAL would be 9 (=length(val)+length('99')) by default rather than 7 with the previous method. The preliminary assignment statement new_val=val or a LENGTH statement could avoid that.

View solution in original post

5 REPLIES 5
MarkusWeick
Barite | Level 11

Hi @Nietzsche , in your example you can fix it with

data news;
val='06MAY98';
new_val=cat(substr(val, 0, 5),99);

proc print data=news; run;

two remarks:

1. "substr" is a function to get a substring not to change a substring.

2. A general better approach might be to work with dates instead of character value resembling dates.

Please help to keep the community friendly.
Like posts you agree with or like. Mark helpful answers as “accepted solutions”. Generally have a look at https://communities.sas.com/t5/Getting-Started/tkb-p/community_articles
LinusH
Tourmaline | Level 20

As @MarkusWeick suggest, use a numerical date formatted column for this.

data news;
   val = '06May1998'd;
   new_val = intnx('YEAR',val, 1, 'SAME');
   format val new_val date9.;
run;
Data never sleeps
Ksharp
Super User

Use left-side SUBSTR() function:

 

data news;
val='06MAY98';
substr(val, 6, 2) = 99;

proc print data=news; run;
MarkusWeick
Barite | Level 11
Thanks for the left-side SUBSTR() function
Please help to keep the community friendly.
Like posts you agree with or like. Mark helpful answers as “accepted solutions”. Generally have a look at https://communities.sas.com/t5/Getting-Started/tkb-p/community_articles
FreelanceReinh
Jade | Level 19

Hi @Nietzsche,

 

Your result 0 is the Boolean value saying that the equality 98 = 99 is false. (The character string substr(val, 6, 2)='98' was automatically converted to the number 98, see the corresponding note in the log.)

 

The SUBSTR (left of =) function modifies the value of the variable in the first argument, so you should create NEW_VAL first with the old value and then modify it:

data news;
val='06MAY98';
new_val=val;
substr(new_val, 6)='99';
run;

Alternatively, you could define NEW_VAL like this

new_val=substr(val, 1, 5)||'99';

using the SUBSTR (right of =) function. But then the length of NEW_VAL would be 9 (=length(val)+length('99')) by default rather than 7 with the previous method. The preliminary assignment statement new_val=val or a LENGTH statement could avoid that.

Catch up on SAS Innovate 2026

Dive into keynotes, announcements and breakthroughs on demand.

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

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 479 views
  • 7 likes
  • 5 in conversation