DATA Step, Macro, Functions and more

create variables using other variable values

Reply
New Contributor
Posts: 2

create variables using other variable values

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?
N/A
Posts: 0

Re: create variables using other variable values

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?
New Contributor
Posts: 2

Re: create variables using other variable values

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.
N/A
Posts: 0

Re: create variables using other variable values

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?
Ask a Question
Discussion stats
  • 3 replies
  • 131 views
  • 0 likes
  • 2 in conversation