Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating new variables in a loop

Status
Not open for further replies.

Chiefsfan4ever

Technical User
Nov 17, 2008
2
US
I have a question about trying to create a new variable in a loop. The idea is that you don't know how many variables you will need and you want it to create the variables as they go along. Below is some sample code of what I want it to do. Maxcount is the maximum of count. Let's just say Maxcount is 2. So I would want variables with the name Bucket1 through Bucket10. Any ideas?

data work2;
set work1;
Extra=0;
retain extra;
NetCount = Maxcount-extra;
if ( NetCount >0 ) and (NetCount = count) then do;
Bucket||(1+5*(Netcount-1))=Bucket1;
Bucket||(2+5*(Netcount-1))=Bucket2;
Bucket||(3+5*(Netcount-1))=Bucket3;
Bucket||(4+5*(Netcount-1))=bucket4;
Bucket||(5+5*(Netcount-1))=bucket5;
end;
Extra = Extra+1;
run;
 
Hey,
What ewactly are you trying to do? And by this I mean, why do you need to create an unknown number of variables?
One simple way to do this would be:-
Code:
* Get maximum value of count in WORK1 *;
* Multiply this by 5                  *;
* Load it into macro variable MAXCNT  *;
proc sql;
  select 5*max(count)
  into :maxcnt
  from work1
  ;
quit;
%put MAXCOUNT=&MAXCNT;

data work2;
  set work1;
  * create variables as 5byte text strings *;
  length Bucket1-Bucket&maxcnt $5;
run;
If you need the BUCKETn variables to be numerics, change the langth statement accordingly.




Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
I know there are always going to be positions for 5 buckets in each row, but I don't know how many rows there are going to be. I want everything to be on one row with new bucket names.

Example: I have 2 rows with information in all buckets. I then have Buckets 1-5 in row one and Buckets 6-10 in row 2. But when I import it everything is Buckets 1-5 so I want to be able to decipher which buckets are which.
 
Ahhh! It seems like you want to TRANSPOSE the data.
Because you have multiple variables (Bucket1 - Bucket5) you can't easily use proc transpose to achieve this.
So, basically, you need to know how many records are in the dataset, and that, times 5, is the number of buckets you need.
Code:
* Get maximum value of count in WORK1 *;
* Multiply this by 5                  *;
* Load it into macro variable MAXCNT  *;
proc sql;
  select 5*count(*)
  into :maxcnt
  from work1
  ;
quit;
%put MAXCOUNT=&MAXCNT;

data work2;
  set work1 end=eof;
  * create variables as 5byte text strings *;
  length NewBucket1-NewBucket&maxcnt $5;
  retain newbucket1-newBucket&maxcnt;
  array newbucket{&MAXCNT) NewBucket1-NewBucket&maxcnt;
  array bucket{&MAXCNT) Bucket1-Bucket5;

  do i=1 to 5;
    newbucket{i+(_n_-1)*5} = Bucket{i};
  end;

  * Uncomment this line when you're happy with the results;
  *  and this will only output the last record.          *;
  *if eof then output;

run;

I think that this should do what you want.



run;
Code:
Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top