BookmarkSubscribeRSS Feed
progster
Fluorite | Level 6

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

8 REPLIES 8
LinusH
Tourmaline | Level 20

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
Astounding
PROC Star

Inside a macro:

%local i;

%do i=0 %to 120;

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

%end;

data_null__
Jade | Level 19

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
    progster
    Fluorite | Level 6

    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

    JerryLeBreton
    Pyrite | Level 9

    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

    data_null__
    Jade | Level 19

    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

    Tom
    Super User Tom
    Super User

    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

    Tom
    Super User Tom
    Super User

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

    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
    • 8 replies
    • 1119 views
    • 0 likes
    • 6 in conversation