How do I create new variables based on entry in Let statement

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

How do I create new variables based on entry in Let statement

This is somewhat of an odd request, but hopefully someone can help me out.

I need to create new variables for each value entered into a %let statement. Ideally, I would like to have a statement:

%let summary_vars = ____________ where they would then list the summary vars (whether separated by spaces or commas, whichever works). I would then like those to each be a separate variable (can just be numbered), and to work on any number of vars entered.

So, for example:

%let summary_vars = outstanding, exercise, total

WOULD RESULT IN:

summary_vars1 = outstanding

summary_vars2 = exercise

summary_vars3 = total

Does anyone have any ideas?

Thanks!


Accepted Solutions
Solution
‎11-08-2012 04:46 PM
Super User
Super User
Posts: 6,500

Re: How do I create new variables based on entry in Let statement

Still not clear what the input is.  Is it a macro variable?  (that is what a %LET statement can create)

Still not clear what the output is. Do you want a SAS dataset as in the example from Art?  Or just a report?

Assuming that you have an input macro variable that I will call VARLIST here is a simple data step to populate it.

%*  Create an example input ;

%let varlist=total,outstanding,exercise,freq;

* Create dataset WANT using ARRAY statement with initial values ;

data want ;

  array summary_var (%sysfunc(countw("&varlist",%str(,)))) $32

   (%sysfunc(tranwrd("&varlist",%str(,),",")))

  ;

run;


proc print ;

run;

       summary_     summary_      summary_    summary_

Obs      var1         var2          var3        var4

1      total      outstanding    exercise      freq

View solution in original post


All Replies
SAS Super FREQ
Posts: 8,743

Re: How do I create new variables based on entry in Let statement

Hi:

  It's not really clear to me what you are trying to do. How would you use summary_vars1, summary_vars2 and summary_vars3. Would "outstanding", "exercise" and "total" already be variables in a data sets? What code have you tried and what are you trying to achieve? What does your input data look like? And what are your desired results?

cynthia

PROC Star
Posts: 7,363

Re: How do I create new variables based on entry in Let statement

Are you looking for something like the following?:

%let summary_vars = outstanding, exercise, total;

data test;

  input outstanding exercise total;

  cards;

1 2 3

4 5 6

;

data want;

  set test;

  array summary_vars(3);

  _n_=1;

  do while (scan("&summary_vars",_n_) ne "");

    summary_vars(_n_)=vvaluex(scan("&summary_vars",_n_));

    _n_+1;

  end;

run;

Occasional Contributor
Posts: 8

Re: How do I create new variables based on entry in Let statement

Thank you both for your responses.

I apologize for my lack of clarity. There will be empty let statements for users to fill in. The summary_vars are not already variables, they are put in by user. The output should result in one line of results - the headers (summary_var1, summary_var2...) and the results (outstanding, total...). So it should look something like this:Capture.JPG
Arthur - the data test portion is not needed because there is no additional information, but the data want looks promising. I need the array to be dynamic based on the number of items in summary_var %let statment. Then I would just need the values to be read into each summary_var in the array.

Thanks again!

Solution
‎11-08-2012 04:46 PM
Super User
Super User
Posts: 6,500

Re: How do I create new variables based on entry in Let statement

Still not clear what the input is.  Is it a macro variable?  (that is what a %LET statement can create)

Still not clear what the output is. Do you want a SAS dataset as in the example from Art?  Or just a report?

Assuming that you have an input macro variable that I will call VARLIST here is a simple data step to populate it.

%*  Create an example input ;

%let varlist=total,outstanding,exercise,freq;

* Create dataset WANT using ARRAY statement with initial values ;

data want ;

  array summary_var (%sysfunc(countw("&varlist",%str(,)))) $32

   (%sysfunc(tranwrd("&varlist",%str(,),",")))

  ;

run;


proc print ;

run;

       summary_     summary_      summary_    summary_

Obs      var1         var2          var3        var4

1      total      outstanding    exercise      freq

Occasional Contributor
Posts: 8

Re: How do I create new variables based on entry in Let statement

EXACTLY what I wanted. Thank you so much!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 238 views
  • 7 likes
  • 4 in conversation