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?
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.
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.
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;
Use left-side SUBSTR() function:
data news; val='06MAY98'; substr(val, 6, 2) = 99; proc print data=news; run;
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.
CFC_SAS_Communities_400x225.jpg
It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.
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.
Ready to level-up your skills? Choose your own adventure.