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

Hi there,

 

I have data in format similar to the following but with many more columns of "Codes" (Code_01 to Code_20) and corresponding "Indicators" (Indicator_01 to Indicator_20):

 

ID Name Age Code_01 Code_02 Code_03 Indicator_01 Indicator_02 Indicator_03
001 John 42 AAA BBB CCC No No Yes
002 Kathy 83 DDD EEE FFF No No No
003 Joe 83 GGG HHH III No Yes No

 

And need to convert it into something like the following:

 

ID Name Age Code Indicator
001 John 42 AAA No
001 John 42 BBB No
001 John 42 CCC Yes
002 Kathy 83 DDD No
002 Kathy 83 EEE No
002 Kathy 83 FFF No
003 Joe 83 GGG No
003 Joe 83 HHH Yes
003 Joe 83 III No

 

Is there an efficient way to do that?

 

Thanks.
Jason

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

If it is just those two variables it is pretty easy to do with arrays.

data want ;
  set have;
  array _code code_01-code_20;
  array _ind indicator_01-indicator_20;
  do index=1 to dim(_code);
    Code = _code[index];
    Indicator = _ind[index];
    output;
  end;
  drop code_01-code_20 indicator_01-indicator_20;
run;

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

If it is just those two variables it is pretty easy to do with arrays.

data want ;
  set have;
  array _code code_01-code_20;
  array _ind indicator_01-indicator_20;
  do index=1 to dim(_code);
    Code = _code[index];
    Indicator = _ind[index];
    output;
  end;
  drop code_01-code_20 indicator_01-indicator_20;
run;
JasonL
Quartz | Level 8

Great thanks!

PaigeMiller
Diamond | Level 26

There's the %UNTRANSPOSE macro at https://support.sas.com/content/dam/SAS/support/en/sas-global-forum-proceedings/2018/2419-2018.pdf

 

The example on page 2 seems exactly the same type of operation as you are asking about.

--
Paige Miller

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1405 views
  • 1 like
  • 3 in conversation