08-31-2016 10:28 AM
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
08-31-2016 11:00 AM
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;
08-31-2016 12:37 PM
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?
08-31-2016 01:45 PM
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?
08-31-2016 06:25 PM
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
09-01-2016 05:02 PM
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).
09-02-2016 07:59 AM
08-31-2016 12:45 PM