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

It looks like it is as you said a problem with invalid data.

I ran your array but I has the same effect, but I found an important bit in the log:

 

dat_ops95=  dat_ops96=  dat_ops97=  dat_ops98=  dat_ops99=  dat_ops100=  i=101 _ERROR_=1 _N_=1
NOTE: Ungültiges zweites Argument für Funktion SUBSTR in Zeile 2371 Spalte 14. --> translated: Invalid second argument for the function substr


NOTE: Over 100 NOTES, additional NOTES suppressed.
NOTE: Ungültiges zweites Argument für Funktion SUBSTR in Zeile 2371 Spalte 14. --> translated: Invalid second argument for the function substr

 

The data look like this, for the variables ops_ko1 till ops_ko100 and fromatted as $CHAR19. :

 

1234b8:r:20060607

627::20071012

382027:l:20071212

182b::20071615

17293b:r:20081281

 

Thank you very much!

RW9
Diamond | Level 26 RW9
Diamond | Level 26

The error shows you the problem:

dat_ops95=  dat_ops96=  dat_ops97=  dat_ops98=  dat_ops99=  dat_ops100=  i=101 _ERROR_=1 _N_=1

 

There is not data in dat_ops95, or 96 etc.  Therefore any substr() evaluation will fail as lengthn(string)=0 then -7 = -7, invalid for substr().

 

Again, post test data exactly as you have it, as a datastep.  Follow this post:

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

 

It is impossible for me to cover data that you have if I don't know about it (i.e. you post on one column, there is 100 in the log).

 

Put an if around the calculation;

data test1;
  set test;
  array ops_ko ops_ko1-ops_ko100;    
  array dat_ops dat_ops1-dat_ops100;
  do i=1 to 100;
    if lengthn(ops_ko{i}) > 0 then do;
      dat_ops{i}=substr(ops_ko{i},lengthn(ops_ko{i})-7,10);
      ops_ko{i}=tranwrd(ops_ko{i},strip(dat_ops{i}),"");
    end;
  end;

run;
sasseln
Obsidian | Level 7

Great, thank you! It is working just fine.

 

I will read your link for the next time and will post proper test data.

 

ballardw
Super User

What is the rule that excludes data from the row with 627::20071012?

mansour_ibrahim
Obsidian | Level 7
data test;
input  @1 var $17. @18 var1 $20.;
cards;
1234b8:r:20060607 1234b8:r:20060607
152::20071012     627:r:20091012
382027:l:20071212 382027::20051212
182b::20070615    182b::20010615
17293b:r:20081231 17293b:r:20001231
;run;

DATA test1;
format date date1 yymmdd10.; 
set test;
array vr (*) $ var var1; 
array cd (*) $ code code1;
array dt(*) $ date date1;
array pr (*) $ prt1 prt2;  
do i=1 to dim(vr);
   if _N_=1 then RE = PRXPARSE("/\:\d{8}/"); RX=PRXPARSE("/\d+\:{2}\d{8}/");             
   retain RE RX;
   if prxmatch(RX, vr(i)) = 0 then do;
   call PRXSUBSTR(RE,vr(i),START,LENGTH);              
   if START GT 0 then do;                                
   cd{i} = SUBSTRN(vr{i},START + 1,LENGTH - 1);
   dt(i) =input (cd(i),yymmdd10.);
   pr(i)=SUBSTRN(vr(i),1,LENGTH(vr(i)) - LENGTH);
      
   end;
   end;
end;
drop i Start Length RE RX;
run;

 

sasseln
Obsidian | Level 7

Hi mansour_ibrahim,

 

thank you for the code! But now it exclusively cuts the data with an l or r in between.

sasseln
Obsidian | Level 7

@ballardw

This one in the code fo RW9:

 

dat_ops{i}=substr(ops_ko{i},lengthn(ops_ko{i})-7,10);

 

And for mansour_ibrhaim:

 

if _N_=1 then RE = PRXPARSE("/\:\d{8}/"); RX=PRXPARSE("/\d+\:{2}\d{8}/");             
   retain RE RX;
   if prxmatch(RX, ops_ko(i)) = 0 then do;
   call PRXSUBSTR(RE,ops_ko(i),START,LENGTH);              
   if START GT 0 then do;                                
   cd{i} = SUBSTRN(ops_ko{i},START + 1,LENGTH - 1);
   dt(i) =input (cd(i),yymmdd10.);
   pr(i)=SUBSTRN(ops_ko(i),1,LENGTH(ops_ko(i)) - LENGTH);
     

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 21 replies
  • 4231 views
  • 9 likes
  • 8 in conversation