BookmarkSubscribeRSS Feed
Cristy001cr
Calcite | Level 5

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

8 REPLIES 8
ballardw
Super User

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;
Cristy001cr
Calcite | Level 5

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?

ballardw
Super User

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?

Cristy001cr
Calcite | Level 5

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

Cristy001cr
Calcite | Level 5

the last explanation I sent makes you understand the problem?

ballardw
Super User

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).

 

Cristy001cr
Calcite | Level 5

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

Cristy001cr
Calcite | Level 5
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?

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 8 replies
  • 1338 views
  • 0 likes
  • 2 in conversation