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

Hi, 

In a dataset have a column named statatum which contains unique values 0 ,1 ,3 but in repetition.

I would like to create multiple different columns in this dataset such as YAS YES YCS sep, but for all hte unique values. such as YAS0 YES0 YCS0 SEP0 YAS1 YES1 YCS1 SEP1 YAS3 YES3 YCS3 SEP3 . 

 

I require help to create these variables in form of a macro, so that if the unique value changes to 2,3,4 or anything X Y Z it must create the columns similar way. 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

What do you actually have? What do you actually want?

So the suffix 0,1,3 is coming from the variable STRATA.

Where is the prefix YAS, YES, YCS coming from? Is that in another variable?
What value do you want the new variables to have? Is that in another variable?

To just create the structure you could do something like this. First make a TALL dataset . Then TRANSPOSE it.

data tall;
  row=1;
  value=1;
  do prefix='YAS','YES','YCS','SEP';
    do suffix=0,1,3;
       output;
    end;
  end;
run;

proc transpose data=tall out=wide(drop=_name_);
  by row;
  id prefix suffix;
  var value;
run;

image.png

 

View solution in original post

9 REPLIES 9
PaigeMiller
Diamond | Level 26

There are many places in this forum where experts advise against creating wide data sets with data in the column name, such as putting the zero value into YAS0, as these become difficult to analyze, and are not dynamic. Leaving the data in its original format not only makes the subsequent code easier to write, but avoids the problem that you state as "so that if the unique value changes to 2,3,4 or anything X Y Z it must create the columns similar way." And because we avoid this problem, a macro is normally not needed.

 

So ... my advice is to restructure the problem so you don't need a wide data set with data in the variable name. 

 

If you explain more about what you plan to do with this data, we could definitely be more specific.

--
Paige Miller
sahoositaram555
Pyrite | Level 9
Thanks @PaigeMiller, sole intention of creating large dataset is to create use them as arguments in my proc template procedure. my intention is to create a survival plot where all these 3 types are 3 different strata. For each strata types, i need to use YAS &YCS in layout overlay as x & y, while others to be used in markercharacter options of proc template.
,

PaigeMiller
Diamond | Level 26

As far as I can see, survival plots are much more easily done on long data sets rather than wide data sets. So again, I urge you to not proceed with your plan as stated and reformulate the problem to use a long data set.

--
Paige Miller
sahoositaram555
Pyrite | Level 9
I do agree! but he scenario is not only alone on survival plot, few modifications in the survival plot for which i have crated a template. if you please could look below to the code by which i'm trying to achieve the code and help me to automate,I would be grateful.
%macro hhh();
%do i =%scan() %to &n_categories; **n_categories has all my unique values(0,1,3) sepated by space
proc transpose data= have;
id &&var&i.;
var statatum ;
run;
%end;
%mend;
%hhh();
PaigeMiller
Diamond | Level 26

@sahoositaram555 wrote:
I do agree! but he scenario is not only alone on survival plot, few modifications in the survival plot for which i have crated a template. if you please could look below to the code by which i'm trying to achieve the code and help me to automate,I would be grateful.
%macro hhh();
%do i =%scan() %to &n_categories; **n_categories has all my unique values(0,1,3) sepated by space
proc transpose data= have;
id &&var&i.;
var statatum ;
run;
%end;
%mend;
%hhh();

From this, there is not enough information to know what you are doing or why you are doing this or where it is going. And so I will not comment on specific code at this time. I also point out that you continually arrive at a macro solution, and I simply am not in agreement.

 

To determine the best structure of the data and a reasonable approach, the details must be provided about this survival plot and template, and I think we'd also need to see a portion of the data.

--
Paige Miller
Tom
Super User Tom
Super User

What do you actually have? What do you actually want?

So the suffix 0,1,3 is coming from the variable STRATA.

Where is the prefix YAS, YES, YCS coming from? Is that in another variable?
What value do you want the new variables to have? Is that in another variable?

To just create the structure you could do something like this. First make a TALL dataset . Then TRANSPOSE it.

data tall;
  row=1;
  value=1;
  do prefix='YAS','YES','YCS','SEP';
    do suffix=0,1,3;
       output;
    end;
  end;
run;

proc transpose data=tall out=wide(drop=_name_);
  by row;
  id prefix suffix;
  var value;
run;

image.png

 

sahoositaram555
Pyrite | Level 9
Thanks @Tom! to answer your all qustions: A) suffix 0,1,3 comes from the column statatum from HAVE dataset . B) YAS','YES','YCS','SEP' are my own defined column names that i would like to create. C)Values i need to define conditionally, many if else conditions based on these column names
The way you have created this is wonderful. Will save this. ONE ADVICE I NEED FURTHER ON THIS IS HOW TO AUTOMATE STEP IN YOUR CODE do suffix=0,1,3;
if this part is automated by passing 0 ,1 , 3 automated not by hard coding then at could use it for my proc template.
Tom
Super User Tom
Super User

If you have a macro variable like:

%let strata = 0,1,3 ;

Then just use that in your DO statement.

do suffix=&strata ;

Or make a dataset with one observation per value.

proc sort data=have (keep=strata) out=suffix nodupkey;
  by strata;
run;

And use the dataset instead of the DO loop.

data tall;
  set suffix ;
  do prefix='YAS',.....;
     ... calculate something here ... 
    output;
  end;
run;
sahoositaram555
Pyrite | Level 9
Thanks @Tom, i have took the distinct values by proc sql and stored them in a macro .Later with reference to your last post I've simply followed the proc transpose procedure to o the Job.

Thanks again for looking patiently to the query and helping me building the concept!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 9 replies
  • 2617 views
  • 2 likes
  • 3 in conversation