deal with numbers as column names in macro, arrays ,do loops

Reply
Contributor
Posts: 27

deal with numbers as column names in macro, arrays ,do loops

hi guys

I would like to create a macro or array to deal with columns called  '0'n to '120'n.

I would like to introduce a loop or somenthing, like do i=0 to 120; but I can't manage to adapt it  to this kind of expression ' ..number that gives the name to the column... 'n

Super User
Posts: 5,254

Re: deal with numbers as column names in macro, arrays ,do loops

Can you be bit more specific about what you wish to do?

Have tried something which didn't work, please attach any relevant log.

Data never sleeps
Super User
Posts: 5,071

Re: deal with numbers as column names in macro, arrays ,do loops

Inside a macro:

%local i;

%do i=0 %to 120;

  %unquote(%str(%'&i%'n))

%end;

Respected Advisor
Posts: 3,777

Re: deal with numbers as column names in macro, arrays ,do loops

I might be inclined to use the nliteral function.  Although my example has no basis in reality.:smileysilly:

17         %macro main;
18            %local i;
19            %do i = 1 %to 10;
20               %sysfunc(nliteral(&i))
21               %end;
22            %mend;
23         data _null_;
24            array a
  • %
  • main (1:10);
    25            put 'NOTE: ' (a
  • )(=);
  • 26            run;

    NOTE:
    '1'n=1 '2'n=2 '3'n=3 '4'n=4 '5'n=5 '6'n=6 '7'n=7 '8'n=8 '9'n=9 '10'n=10
    Contributor
    Posts: 27

    Re: deal with numbers as column names in macro, arrays ,do loops

    Sorry guys I can't figure out to apply your statement to my real life situation

    my real data have a structure like this:

    data net_revenues;

      infile cards dlm=',';

      input type $ '1'n '2'n '3'n;

      cards;

    a,18,12,100

    b,20,23,50

    c,22,1,75

    ;

    in real life I have many more columns so as I said I would like to avoid to repeat all the column name.

    let's imagine a simple macro putting a proc freq of variable  '1'n to '3'n? how would you adapt your code to this (in real life I would do thing more complex than proc freq, but it's just a workaround to speak of something easy).

    thanks

    Frequent Contributor
    Posts: 85

    Re: deal with numbers as column names in macro, arrays ,do loops

    Can you take a step back? 

    Why can't you simply name your variables N0, N1, ..., N120

    This would make array processing and looping straight forward.

    The variables can also just be given labels 0, 1, ..., 120

    Respected Advisor
    Posts: 3,777

    Re: deal with numbers as column names in macro, arrays ,do loops

    I did say it had no basis in reality.  I think you just need to use a "SAS Variable List"

    enumerated '1'n-'300'n

    name range '1'n-numeric-'200'n

    Super User
    Super User
    Posts: 6,497

    Re: deal with numbers as column names in macro, arrays ,do loops

    Here is code to input an arbitrary number of columns named with numbers.

    I still have no idea why you would want to do this.


    options validvarname=any;

    %let first=1;

    %let last=3;


    data net_revenues;

      infile cards dlm=',' truncover ;

      input type $ "&first"n - "&last"n;

      put (_all_) (=);

      cards;

    a,18,12,100

    b,20,23,50

    c,22,1,75

    ;

    type=a 1=18 2=12 3=100

    type=b 1=20 2=23 3=50

    type=c 1=22 2=1 3=75

    Super User
    Super User
    Posts: 6,497

    Re: deal with numbers as column names in macro, arrays ,do loops

    Why do you think that you need to use numbers for your variable names?

    Ask a Question
    Discussion stats
    • 8 replies
    • 407 views
    • 0 likes
    • 6 in conversation