- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 05-05-2008 10:49 AM
(1704 views)
Hi,
I want to create n new variables (within a DataSet) where n is the value of a variable nbr.
The following code doesn't work, but may illustrate the problem:
data temp;
set temp;
by origin destin day std;
where nbr ne 1;
if last.origin or last.destin or last.day then do;
call symput ('nbr', compress(NBR));
do J = 1 to NBR;
call symput ('j', compress(J));
call symput ('name', TEST_||compress(J));
resolve('&name.') = lag||resolve('&j.')(std);
end;
output;
end;
run;
Is there a way you can use dataset values within the names of new variables?
I want to create n new variables (within a DataSet) where n is the value of a variable nbr.
The following code doesn't work, but may illustrate the problem:
data temp;
set temp;
by origin destin day std;
where nbr ne 1;
if last.origin or last.destin or last.day then do;
call symput ('nbr', compress(NBR));
do J = 1 to NBR;
call symput ('j', compress(J));
call symput ('name', TEST_||compress(J));
resolve('&name.') = lag||resolve('&j.')(std);
end;
output;
end;
run;
Is there a way you can use dataset values within the names of new variables?
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
First, there is no need for
[pre]
if last.origin or last.destin or last.day then do;
[/pre]
I believe it is redundant in intent to simply
[pre]
if last.day then do;
[/pre]
If not, then for every observation that has the same value for "origin" as the last "origin" value this thing will run.
Next,
I think you may want to rethink through what you are trying to do.
Are you really wanting to create an array of variables for each observation?
What your code is trying to do is create a ton of macro variables. What for?
If you have 100 observations with unique names, and nbr = 10, then your request would be to create 1000 variables in the dataset. You would end up with 1000 variables for each observation (record) and 90% of the variables would have missing values. Is that really what you want? Why would you want to do this?
Are you confused about what SAS is and how it works as a data processing language?
[pre]
if last.origin or last.destin or last.day then do;
[/pre]
I believe it is redundant in intent to simply
[pre]
if last.day then do;
[/pre]
If not, then for every observation that has the same value for "origin" as the last "origin" value this thing will run.
Next,
I think you may want to rethink through what you are trying to do.
Are you really wanting to create an array of variables for each observation?
What your code is trying to do is create a ton of macro variables. What for?
If you have 100 observations with unique names, and nbr = 10, then your request would be to create 1000 variables in the dataset. You would end up with 1000 variables for each observation (record) and 90% of the variables would have missing values. Is that really what you want? Why would you want to do this?
Are you confused about what SAS is and how it works as a data processing language?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
No, I'm not confused and yes there is actually need for the three different 'last.'.
But this is not supposed to create 1000 new variables. There is another variable 'nbr', which is unique within each group an defines the number of variables to be created. If the maximum value of number (over all obs) is for example 10, then there will not more then 10 new variables created, since they are all named the same just using the linenbr with each group as an index.
You can solve the problem with sorting and using lag by taking the max of nbr and then deleting the 'wrong' values due to the group value of 'nbr'. But I'm looking for a straight forward way and not such a workaround.
But this is not supposed to create 1000 new variables. There is another variable 'nbr', which is unique within each group an defines the number of variables to be created. If the maximum value of number (over all obs) is for example 10, then there will not more then 10 new variables created, since they are all named the same just using the linenbr with each group as an index.
You can solve the problem with sorting and using lag by taking the max of nbr and then deleting the 'wrong' values due to the group value of 'nbr'. But I'm looking for a straight forward way and not such a workaround.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Let's attack this problem from a different angle.
Are you trying to transpose the data in a dataset set so that X observations are translated into X array elements?
Are you trying to transpose the data in a dataset set so that X observations are translated into X array elements?