DATA Step, Macro, Functions and more

Create Id Variable aaa---> zzz Array and Do Loop.

Reply
N/A
Posts: 0

Create Id Variable aaa---> zzz Array and Do Loop.

I don't know if I am posting this in the right spot or not but I need some help. I am trying to make 17576 id variables ranging from aaa to zzz, that goes aab aac...aba abb ... zaa... zzz. I am trying to use array statements and do loops to do this but im not sure how to get it to work right. What I have right now is:

ARRAY x[26] $1. ('a' ... 'z'); * All letters a thru z.
ARRAY y[26] $1. ('a' ... 'z');
ARRAY z[26] $1. ('a' ... 'z');
ARRAY id[3] $1. ('x' 'y' 'z');

id = x || y || z ;

Do i = 1 to 17576;
Output;
End;
Run;

I get an error of array subscript out of range. I think this is when i hits 27 it runs out of letters and doesn't know what to do. I am not sure where to go from here. Maybe three seperate do loops?? Any help would be greatly appreciated. Thanks!!
N/A
Posts: 0

Re: Create Id Variable aaa---> zzz Array and Do Loop.

Posted in reply to deleted_user
I don't know why but when i posted it changed my id = statement. I currently have brackets behind each one with an i in the brackets ( )

id{i} = x{i} || y{i} || z{i} ; Message was edited by: SASuser01
SAS Super FREQ
Posts: 8,868

Re: Create Id Variable aaa---> zzz Array and Do Loop.

Posted in reply to deleted_user
Hi:
Well, when the array index got to be 27, since you declared the array to be a size of 26, there was no array member at 27, 28, 29, 30...etc, etc. Also, your OUTPUT statement needed to be inside the loop in order to output each unique IDVAR.

However, you don't really need an array to create your IDVAR. A do loop can iterate over a list of character constants. That is the method I use, frequently, to generate test data.

[pre]
data testit;
length idvar $3;
do i = 'a','b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z';
do j = 'a','b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z';
do k = 'a','b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z';
idvar = catt(i,j,k);
output;
end;
end;
end;
run;

ods listing;
proc print data=testit;
run;
[/pre]

cynthia
N/A
Posts: 0

Re: Create Id Variable aaa---> zzz Array and Do Loop.

Posted in reply to deleted_user
Thanks, that was very helpful. I do have another question now however. I am importing a csv file. I have to edit the data that I am pulling from the file but only have expierience with data files. Is there a way to convert the csv file into a sas data file that is delimited by commas?
Super Contributor
Super Contributor
Posts: 3,174

Re: Create Id Variable aaa---> zzz Array and Do Loop.

Posted in reply to deleted_user
In the future, please send a new post to the forum with each new question. For your second question, you can use PROC IMPORT to read your CSV file, if you want SAS to do the processing for you, otherwise you can use the DATA step technique, with INFILE statement having a DSD and DLM=',' keyword, followed by your own INPUT statement to read the CSV-formatted input file.

The SAS support website http://support.sas.com/ and its SEARCH facility can help identify SAS product documentation and also technical topic reference papers on the use of PROC IMPORT or reading an input CSV-formatted (delimited) file using a DATA step.

Scott Barry
SBBWorks, Inc.
SAS Super FREQ
Posts: 8,868

Re: Create Id Variable aaa---> zzz Array and Do Loop.

Posted in reply to deleted_user
Hi:
This really is a different question. However, the idea that a "sas data file is delimited by commas" is a foreign concept. SAS data files or data sets are stored in a proprietary file structure, that is basically tabular in structure. SAS data sets are composed of ROWS (or OBSERVATIONS) and COLUMNS (or VARIABLES). SAS data sets do not have delimiters. You may have a VARIABLE called FULLNAME, which contains values like these:
Public, John Q.
Phrog, Kermit T.
Phant, Elle E.

however, that would not be considered a "delimited" data file. That is a variable value of the form
LastName, First Middle

When you say you have to "edit the data" that you are pulling from the file, one possible scenario might be:
1) read the CSV file into a SAS dataset

2) Then, manipulate the data in the SAS dataset with the SAS programming language to (hypothetically):
2a) divide all annual salary values by 12 to create a monthly salary variable
2b) do some data cleansing and make sure that the states are all 2 character codes without punctuation (N.C. becomes NC; M.D. becomes MD, etc)
2c) do some other data manipulation

3) create a NEW CSV file from the now-edited SAS dataset as the final output after the data cleansing.

Reading the documentation on what a SAS data set is might help you come up with a better idea of the type of processing you want to perform and what your final output will be -- either SAS data set or an ASCII CSV file.

cynthia
Regular Contributor
Posts: 229

Re: Create Id Variable aaa---> zzz Array and Do Loop.

Posted in reply to deleted_user
Good work
Ask a Question
Discussion stats
  • 6 replies
  • 841 views
  • 0 likes
  • 4 in conversation