BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SasPerson85
Calcite | Level 5

So I have a table that looks like this:

project_numdepartmentdty_startdty_endamount_contrib_yearsyear_2015year_2016year_2017year_2018year_2019year_2020year_2021year_2022
30330431Sociology201720183500        
32310125Sociology2018202238667.8        
30330520Sociology201820193750        
32310118Sociology2015201819250        
30330494Sociology201820192225.5        
32310132Sociology20192020575        
30330508Sociology201820192096        

 

I need the table to look like this:

 

project_numdepartmentdty_startdty_endamount_contrib_yearsyear_2015year_2016year_2017year_2018year_2019year_2020year_2021year_2022
30330431Sociology201720183500  35003500    
32310125Sociology2018202238667.8   38667.838667.838667.838667.838667.8
30330520Sociology201820193750   37503750   
32310118Sociology201520181925019250192501925019250    
30330494Sociology201820192225.5   2225.52225.5   
32310132Sociology20192020575    575575  
30330508Sociology201820192096   20962096   

 

So basically I need to copy the values from "amount_contrib_years" to the appropriate year column. So for example, the top record of the table dty_start = 2017 and dty_end = 2018 so year_2017 and year_2018 both get the value 3500.

 

Also the year_xxxx fields have been generated using proc tabulate by taking the min(dty_start) and max(dty_end) in macro variables. That way those variables will always have the correct year range listed. Maybe I can do something similar for these values but I am not to sure how.

 

Thank you for help in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @SasPerson85  Please see if this helps

 

data have;
infile cards truncover;
input project_num :$15.	department :$15.	dty_start	dty_end	amount_contrib_years	year_2015	year_2016	year_2017	year_2018	year_2019	year_2020	year_2021	year_2022;
cards;
30330431	Sociology	2017	2018	3500	 	 	 	 	 	 	 	 
32310125	Sociology	2018	2022	38667.8	 	 	 	 	 	 	 	 
30330520	Sociology	2018	2019	3750	 	 	 	 	 	 	 	 
32310118	Sociology	2015	2018	19250	 	 	 	 	 	 	 	 
30330494	Sociology	2018	2019	2225.5	 	 	 	 	 	 	 	 
32310132	Sociology	2019	2020	575	 	 	 	 	 	 	 	 
30330508	Sociology	2018	2019	2096	 	 	 	 	 	 	 	 
;

data want;
 set have;
 array y(2015:2022) year_2015-year_2022;
 do _n_=dty_start to dty_end;
   y(_n_)=amount_contrib_years;
 end;
run;
project_num department dty_start dty_end amount_contrib_years year_2015 year_2016 year_2017 year_2018 year_2019 year_2020 year_2021 year_2022
30330431 Sociology 2017 2018 3500.0 . . 3500 3500.0 . . . .
32310125 Sociology 2018 2022 38667.8 . . . 38667.8 38667.8 38667.8 38667.8 38667.8
30330520 Sociology 2018 2019 3750.0 . . . 3750.0 3750.0 . . .
32310118 Sociology 2015 2018 19250.0 19250 19250 19250 19250.0 . . . .
30330494 Sociology 2018 2019 2225.5 . . . 2225.5 2225.5 . . .
32310132 Sociology 2019 2020 575.0 . . . . 575.0 575.0 . .
30330508 Sociology 2018 2019 2096.0 . . . 2096.0 2096.0 . .

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20

Hi @SasPerson85  Please see if this helps

 

data have;
infile cards truncover;
input project_num :$15.	department :$15.	dty_start	dty_end	amount_contrib_years	year_2015	year_2016	year_2017	year_2018	year_2019	year_2020	year_2021	year_2022;
cards;
30330431	Sociology	2017	2018	3500	 	 	 	 	 	 	 	 
32310125	Sociology	2018	2022	38667.8	 	 	 	 	 	 	 	 
30330520	Sociology	2018	2019	3750	 	 	 	 	 	 	 	 
32310118	Sociology	2015	2018	19250	 	 	 	 	 	 	 	 
30330494	Sociology	2018	2019	2225.5	 	 	 	 	 	 	 	 
32310132	Sociology	2019	2020	575	 	 	 	 	 	 	 	 
30330508	Sociology	2018	2019	2096	 	 	 	 	 	 	 	 
;

data want;
 set have;
 array y(2015:2022) year_2015-year_2022;
 do _n_=dty_start to dty_end;
   y(_n_)=amount_contrib_years;
 end;
run;
project_num department dty_start dty_end amount_contrib_years year_2015 year_2016 year_2017 year_2018 year_2019 year_2020 year_2021 year_2022
30330431 Sociology 2017 2018 3500.0 . . 3500 3500.0 . . . .
32310125 Sociology 2018 2022 38667.8 . . . 38667.8 38667.8 38667.8 38667.8 38667.8
30330520 Sociology 2018 2019 3750.0 . . . 3750.0 3750.0 . . .
32310118 Sociology 2015 2018 19250.0 19250 19250 19250 19250.0 . . . .
30330494 Sociology 2018 2019 2225.5 . . . 2225.5 2225.5 . . .
32310132 Sociology 2019 2020 575.0 . . . . 575.0 575.0 . .
30330508 Sociology 2018 2019 2096.0 . . . 2096.0 2096.0 . .
SasPerson85
Calcite | Level 5
Thank you so much for the quick reply and easy to understand solution! I plugged my macro variables into it that gets the min and max of the years and it worked seamlessly!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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