BookmarkSubscribeRSS Feed
LightJade
Calcite | Level 5

Hi! 

I need some help on how to do below transformation (from table 1 to table 2), 

 

In my example in table 1, student 1 has been in City02, City03 and City250 of the 250 cities, so I put '1' for each of the 3 cities he went to. 

Student 2 has been in City01, City03 of the 250 cities, so I put '1' for each of the 2 cities he went to. 

 

In my output table 2, I want to list all the cities the students went to in one column and omit the cities that they never visited. 

 

Can anyone give me help how to get table 2 as the output dataset from table 1 in sas?

Greatly appreciate!

Table 1     
 City01City02City03City250
Student 101101
Student 210100

 

 

Table 2 
 City
Student 1City02
Student 1City03
Student 1City250
Student 2City01
Student 2City03
3 REPLIES 3
LightJade
Calcite | Level 5

Hi KurtBremser, thank you so much for above reply. Sorry I was tweaking my original data tables a bit before the posting, so when I applied your code to  my true data, the output gives 0 obs.

 

So I reorganized my data tables in below way to be exactly the same as my true data 

 

Table 1     
Student_IDCity001City002City003City250
A1F2301101
MN19B10100

 

Table 2 
Student_IDCity
A1F23City002
A1F23City003
A1F23City250
MN19BCity01
MN19BCity03

 

May I have your insight how to tweak your above code to make it work?

Thank you very much!!

Kurt_Bremser
Super User

Just use the correct variable name (student_id) in the BY statement.

You can avoid such misunderstandings (because I had to make assumptions about your data) by posting data in the proper manner (as SAS code, data steps with datalines).

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 490 views
  • 0 likes
  • 2 in conversation