BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
1 ACCEPTED SOLUTION

Accepted Solutions
tmlee02
Fluorite | Level 6

Instead of using an array, you could also edit your DATA step to use the following 

        

         IF  Year = '2009' THEN Yr2009 = HtFt;

           

       

View solution in original post

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

It can be done much simpler with an array using the years of interest as indexes

 

data TreesWide2;
   set Trees;
   array _ {2009 : 2012} _2009 - _2012;
   do until (last.Type);
      set Trees;
      by Type;
      _ [Year] = HtFt;
   end;
run;

Result:

 

Type   Year  HtFt _2009 _2010 _2011 _2012 
Aspen  2010  16    15   16    .     . 
Maple  2012  10    .    6     8     10 
Spruce 2012  25    22   23    24    25 
ballardw
Super User

What is wrong with using a proper tool like Proc Transpose?

 

When you have errors it is best practice to copy the log information with the entire data step or procedure code and all notes, warnings and errors. Paste the copied text into a code box opened on the forum with the </> icon to preserve formatting and appearance of the diagnostics that SAS often supplies with errors.

 

 

You are referencing 4 variables that do not exist in your data: yr2009, yr2010 etc. So they have no values.

Your variable HtFt is never text and has values like 15, 16, 20, not 2009, 2010, 2011 or 2012. So you are 1) going to get conversion messages in the log and 2) none of the comparisons will ever be true so all of your _2009 etc variables will always be missing.

tmlee02
Fluorite | Level 6

Instead of using an array, you could also edit your DATA step to use the following 

        

         IF  Year = '2009' THEN Yr2009 = HtFt;

           

       

adhunamukherjee
Fluorite | Level 6

Hi Ruth,

I am not sure if you have found the solution already. The below code runs fine for me:

 

 

DATA	WORK.TreesWide2;
	SET	Work.Trees ;
	BY	Type;

	RETAIN	_2009 _2010 _2011 _2012;

	IF	FIRST.Type = 1	THEN	CALL MISSING (_2009, _2010, _2011 , _2012);
	IF			Year = '2009' THEN _2009 = HtFt;
		ELSE IF Year = '2010' THEN _2010 = HtFt;
		ELSE IF Year = '2011' THEN _2011 = HtFt;
		ELSE IF Year = '2012' THEN _2012 = HtFt;

	IF	LAST.Type = 1;
    DROP Year HtFt;
RUN;


This generated the following wide dataset:

Type_2009_2010_2011_2012
Aspen1516..
Maple.6810
Spruce22232425


Please let me know if this doesn't work for you.

 

Thanks,

Adhuna

 

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 1247 views
  • 4 likes
  • 5 in conversation