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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

5 REPLIES 5
Cynthia_sas
SAS Super FREQ

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

art297
Opal | Level 21

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;

BN_RN17
Calcite | Level 5

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!

Tom
Super User Tom
Super User

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

BN_RN17
Calcite | Level 5

EXACTLY what I wanted. Thank you so much!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 689 views
  • 7 likes
  • 4 in conversation