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

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