BookmarkSubscribeRSS Feed
AmirSari
Quartz | Level 8

Hi Everyone,

 

I am trying to create a variable by id in which the value of the variable is calculated based on another variable and is repeated 10 times for each id. Here is an example of my dataset and my desired variables, i.e. var1, var2, and var3, as well as the calculations for each one of them. I have also attached the dataset in SAS format.

 

idgwillLag_gwillyearvar1var2 var3
1100 200010 = 100/10  
1150100200110 = 100/105 = (150-100)/10 
1130150200210 = 100/105 = (150-100)/10-2 = (130-150)/10
1140130200310 = 100/105 = (150-100)/10-2 = (130-150)/10
1150140200410 = 100/105 = (150-100)/10-2 = (130-150)/10
1120150200510 = 100/105 = (150-100)/10-2 = (130-150)/10
1110120200610 = 100/105 = (150-100)/10-2 = (130-150)/10
1100110200710 = 100/105 = (150-100)/10-2 = (130-150)/10
1140100200810 = 100/105 = (150-100)/10-2 = (130-150)/10
1160140200910 = 100/105 = (150-100)/10-2 = (130-150)/10
1180160201005 = (150-100)/10-2 = (130-150)/10
1110180201100-2 = (130-150)/10
11501102012000
2200 200320 = 200/10  
2200200200420 = 200/100 = (200-200)/10 
2180200200520 = 200/100 = (200-200)/10-2 = (180-200)/10
2150200200620 = 200/100 = (200-200)/10-2 = (180-200)/10
2150150200720 = 200/100 = (200-200)/10-2 = (180-200)/10
2140150200820 = 200/100 = (200-200)/10-2 = (180-200)/10
2170140200920 = 200/100 = (200-200)/10-2 = (180-200)/10
2190170201020 = 200/100 = (200-200)/10-2 = (180-200)/10
2215190201120 = 200/100 = (200-200)/10-2 = (180-200)/10
2230215201220 = 200/100 = (200-200)/10-2 = (180-200)/10
2165230201300 = (200-200)/10-2 = (180-200)/10
2182165201400-2 = (180-200)/10

 

 

Thanks very much for your help.

4 REPLIES 4
andreas_lds
Jade | Level 19

Can you, please post what you have as text: a data step using datalines? Datasets are binary files and i am not allowed to download them. You could highlight those variables, that you already have in the table, alternatively.

AmirSari
Quartz | Level 8

here is what I have. I want to create var1, var2, and var3 in my original post.

 

data ka.gwill;
input id gwill lag_gwill year;
cards;
1 100 . 2000
1 150 100 2001
1 130 150 2002
1 140 130 2003
1 150 140 2004
1 120 150 2005
1 110 120 2006
1 100 110 2007
1 140 100 2008
1 160 140 2009
1 180 160 2010
1 110 180 2011
1 150 110 2012
2 200 . 2003
2 200 200 2004
2 180 200 2005
2 150 200 2006
2 150 150 2007
2 140 150 2008
2 170 140 2009
2 190 170 2010
2 215 190 2011
2 230 215 2012
2 165 230 2013
2 182 165 2014
;
run;

 thanks

andreas_lds
Jade | Level 19

Surely not the most elegant solution:

data want;
   set work.gwill;
   by id;
   
   retain start_year var1-var3;
   
   if first.id then do;
      start_year = year;
   end;
   
   select (year);
      when (start_year) var1 = gwill / 10;
      when (start_year + 1) var2 = (gwill - lag_gwill) / 10;
      when (start_year + 2) var3 = (gwill - lag_gwill) / 10;
      when (start_year + 11) var1 = 0;
      when (start_year + 12) var2 = 0;
      when (start_year + 13) var3 = 0;
      otherwise;
   end;
  
   drop start_year;
run;
AmirSari
Quartz | Level 8

Thanks, andreas_Ids for your solution. It does work for my example here. However, when I dropped the last observation for id=1, the value of var3 from id=1 is copied over to id=2. I am not sure why the first.id statement is ignored in this case. If you run the following code, you can see that the value of var3 id=1 is copied to var3 id=2 for the first two observations which are supposed to be zero.

data gwill;
input id gwill lag_gwill year;
cards;
1 100 . 2000
1 150 100 2001
1 130 150 2002
1 140 130 2003
1 150 140 2004
1 120 150 2005
1 110 120 2006
1 100 110 2007
1 140 100 2008
1 160 140 2009
1 180 160 2010
1 110 180 2011
2 200 . 2003
2 200 200 2004
2 130 200 2005
2 150 200 2006
2 150 150 2007
2 140 150 2008
2 170 140 2009

;
run;

data want;
set gwill;
by id;

retain start_year var1-var3;

if first.id then do;
start_year = year;
end;

select (year);
when (start_year) var1 = gwill / 10;
when (start_year + 1) var2 = (gwill - lag_gwill) / 10;
when (start_year + 2) var3 = (gwill - lag_gwill) / 10;
when (start_year + 10) var1 = 0;
when (start_year + 11) var2 = 0;
when (start_year + 12) var3 = 0;
otherwise;
end;

drop start_year;
run;

 

Any ideas why this is happenings?

Thanks! 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 4 replies
  • 582 views
  • 1 like
  • 2 in conversation