BookmarkSubscribeRSS Feed
Niels
Calcite | Level 5
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?
3 REPLIES 3
deleted_user
Not applicable
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?
Niels
Calcite | Level 5
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.
deleted_user
Not applicable
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?

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 752 views
  • 0 likes
  • 2 in conversation