0

Let's say I have two columns A and B.

A     B
12    "randstr"
39    "randstr"
2     "randstr"

This random string is repeated in each row.

I'm interested in how I can get the table below:

randstr     B
12          "randstr"
39          "randstr"
2           "randstr"

The value in the column B was used to rename column A. I have tried using rename and all sorts of macro magic but failed. I have no idea how to proceed.

I've tried the answers below and they just don't allow for reading the value from the data and then using the value as a column name:

  1. https://communities.sas.com/t5/General-SAS-Programming/dates-used-as-column-names/td-p/168803
  2. https://stats.idre.ucla.edu/sas/code/a-few-sas-macro-programs-for-renaming-variables-dynamically/
  3. SAS - Dynamically create column names using the values from another column
  4. Renaming Column with Dynamic Name
3
  • Is this a homework question, this is not the first time today I've seen it/ Anyways, you're looking for how to create a format. Google a paper called: PROC FORMAT not just another pretty face and it has some really good examples. Commented Feb 14, 2018 at 20:46
  • If you want actual code help, your question doesn't follow SO rules, you need to show what you've tried, not just say you've tried something. Commented Feb 14, 2018 at 20:47
  • @Reeza I'm pretty sure PROC FORMAT won't work. String is random, I cannot list all of the values. I've added 3 answers I've tried. It's also not homework, I'm doing this at work and have just started to play with macros and thought there might be a way. Could you link me to the question that you've seen repeated? Commented Feb 15, 2018 at 6:18

2 Answers 2

1

The transformation could also be seen as a row-wise transposition.

data have;
attrib A length=8 B length=$32;
row+1;
input
A   & B; datalines;
12    xyz-123-abc
39    xyz-123-abc
2     xyz-123-abc
run;

proc transpose data=have out=want(drop=row _name_);
  by row;
  var A;
  id B;
  copy B;
run;

In non-toy scenarios the B column is often not a single value. Try the same transpose with data having variation in B. The procedure will create two new columns from the values of B.

A   & B; datalines;
12    xyz-123-abc
39    xyz-123-abc
2     xyz-123-abc
3141  xyz-456-def
Sign up to request clarification or add additional context in comments.

Comments

0

Using this macro, it's fairly straightforward:

/* get first value in the dataset */
%let new_col=%mf_getvalue(work.YOURDATA,B);

/* rename variable A */
proc datasets library=work nolist;
 modify YOURDATA;
 rename A=%sysfunc(dequote(&new_col));
quit;

1 Comment

Works! Exactly what I've needed. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.