DATA Step, Macro, Functions and more

create a new variable from one that already exists on the historical series

Reply
Occasional Contributor
Posts: 6

create a new variable from one that already exists on the historical series

I have a SAS data set with a variable called year and other variables that contain numerical values. The year variable contains the years 2000 to 2013 for the data of a group, then start again 14 years always listed from 2000 to 2013 for a second group, I would like to create a new variable and call group to host for the first set value 0 and the second value group 1. How can I do? thanks a lot for example
year var1 var2
2000 1333 122
2001 1444 177 ..
2013 1555 199
2000 1999 555
2001 2000 888
2013 2222 532

Super User
Posts: 10,460

Re: create a new variable from one that already exists on the historical series

Assuming the order stays as shown one way:

 

data want;
   set have;
   retain group 0;
   if dif(year)<0 and _n_>1 then group+1;
run;
Occasional Contributor
Posts: 6

Re: create a new variable from one that already exists on the historical series

thanks for your answer, just by running the code I created a variable called Group with numbers 1 2 3 up to 28. The year variable with missing values. I would like to have a group variable with only 0 for the first 14 years and 1 for the second 14 years. Now after I wrote this piece of code, how do I do?

Super User
Posts: 10,460

Re: create a new variable from one that already exists on the historical series

You need to provide more example data and the results. Your "first 14 years" and "second 14 years" do not have a clear definition. Do you mean the the first 14 RECORDS regardless of the values of the year variable?

Occasional Contributor
Posts: 6

Re: create a new variable from one that already exists on the historical series

The contents in the variable for example 1 2 3 etc. all refer to the value of the variable year. The data I obtained them from an Excel spreadsheet, the first 14 years, from 2000 2001 and so on up to 2013 (each year has other variables with economic values) belong to a group and the other 14 years from 2000 to 2013 come from an another source to do statistical checks
In SAS data sets I have the first 14 years, one for each observation and related variables, after the record year 2013 belongs to the first group, the second group starts from 2000 2001 and so on until 2013, so I have to create a  classification variable to which I intend to assign 0 for the first group and 1 for the second, otherwise I can not distinguish

Occasional Contributor
Posts: 6

Re: create a new variable from one that already exists on the historical series

the last explanation I sent makes you understand the problem?

Super User
Posts: 10,460

Re: create a new variable from one that already exists on the historical series

It may help to provide actual data, or if the actual data is too sensitive something with the exact same structure of the key variables. This link https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... shows how to turn a dataset into SAS data step code that can be pasted here to recreate your data for testing.

 

It would help to show what that same data should look like with the added variables. You probably only need to show two cycles of the data (assuming there are more in the dataset).

 

Occasional Contributor
Posts: 6

Re: create a new variable from one that already exists on the historical series

I will send the data set, as you see I have two groups, and I would like to create a group variable for the first giving the value 0 and the second value group 1.
thank you

Occasional Contributor
Posts: 6

Re: create a new variable from one that already exists on the historical series

thanks for your answer, just by running the code I created a variable called Group with numbers 1 2 3 up to 28. The year variable with missing values. I would like to have a group variable with only 0 for the first 14 years and 1 for the second 14 years. Now after I wrote this piece of code, how do I do?
Ask a Question
Discussion stats
  • 8 replies
  • 488 views
  • 0 likes
  • 2 in conversation