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

We have the following problem. We want SAS to put the same value that is inside the last beruf variable that contains info into the next one  if the control variables Übergänge and BERUFG_A_1 match. There are beruf1 to beruf 21 so 21 variables. If we use this code it says array subscript out of range.

LIBNAME WB66 'D:\WB66';
RUN;

DATA WB66.erwzus_1013; SET WB66.erwzus_1013;

array beruf (1:21) $ beruf1-character-beruf21;

do i = 1 to dim(beruf);
if Übergänge = 2 and BERUFG_A_1=1 and i = index then beruf{i} = beruf{i-1};
end;

RUN;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

The messages in the log should show the value of I used when the error happened.

It should be happening when I=1 since you have beruf{i-1} in your code.

Change your DO loop starting from 1 to 2 if you want to do that.

do i = 2 to dim(beruf);

View solution in original post

12 REPLIES 12
Tom
Super User Tom
Super User

The messages in the log should show the value of I used when the error happened.

It should be happening when I=1 since you have beruf{i-1} in your code.

Change your DO loop starting from 1 to 2 if you want to do that.

do i = 2 to dim(beruf);
Gablz
Obsidian | Level 7

Thank you! It works now 😄

andreas_lds
Jade | Level 19

Please post the complete log using "insert code", also note that "DATA WB66.erwzus_1013; SET WB66.erwzus_1013;" is always a very bad idea, if something went wrong in the step, you have to re-create WB66.erwzus_1013.

Gablz
Obsidian | Level 7
Hey 🙂
Thanks for your reply. I tried DATA WB66.erwzus_1013; SET WB66.erwzusneu; but it doesnt make a new file. Do you know what i could use instead? Or do i have to create an empty file first?
PaigeMiller
Diamond | Level 26

I tried DATA WB66.erwzus_1013; SET WB66.erwzusneu; but it doesnt make a new file.


Was there already a existing file named WB66.erwzus_1013??

--
Paige Miller
Gablz
Obsidian | Level 7
Yes there was and it will always get replaced
PaigeMiller
Diamond | Level 26

@Gablz wrote:
Yes there was and it will always get replaced

That's what your code is telling SAS to do. If you want a new file to be created, you need to use a file name that doesn't exist. For example:

 

DATA WB66.erwzus_1013new; 
SET WB66.erwzusneu;
--
Paige Miller
Gablz
Obsidian | Level 7
But i also want the program to use Data from an existing file so i want him to take the information out of WB66.erwzus_1013. If i do it that way SAS tells me the file doesnt exist
Gablz
Obsidian | Level 7
Maybe thats a little bit confusing but there already is WB66.erwzus_1013 with information that i want to work with and i want the program to make a new file and he should put everything that i change into that file like a copy but with the changes
Tom
Super User Tom
Super User

@Gablz wrote:
Maybe thats a little bit confusing but there already is WB66.erwzus_1013 with information that i want to work with and i want the program to make a new file and he should put everything that i change into that file like a copy but with the changes

The names of dataset(s) listed in the DATA statement are the target names.  The names of dataset(s) listed in the SET statement are the source datasets.

 

The comment that triggered the discussion was that you should be very careful about using the same name in both places because if there is a problem then your source dataset might be lost.

 

Instead get in the habit of making a new dataset to and only read from the original dataset.  The new dataset will have all of the data from the old datasets with any changes made to it in this data step.

 

data new;
  set old;
  *** more statements that manipulate the data ** ;
run;
Gablz
Obsidian | Level 7
Oh okay i thought there was a command for that thank you 🙂
PaigeMiller
Diamond | Level 26

@Gablz wrote:
But i also want the program to use Data from an existing file so i want him to take the information out of WB66.erwzus_1013. If i do it that way SAS tells me the file doesnt exist

 I think this is what you want

 

DATA WB66.erwzus_1013new; 
SET WB66.erwzus_1013;

 

 

--
Paige Miller

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 12 replies
  • 1779 views
  • 7 likes
  • 4 in conversation