DATA Step, Macro, Functions and more

Is loop possible here?

Reply
New Contributor
Posts: 4

Is loop possible here?

If opprim_lak=. then opprim_lak=0;
I need to do this 120 times. Do anyone know if there is any shortcut or do I have to copy this 120 times? The variables are in a sequence in the dataset beginning with "opprim_lak" and ending with the last variable "sluten_lak"?
Contributor
Posts: 31

Re: Is loop possible here?

Hi Janne,
As the variables are in sequence in dataset, beginning with opprim_lak till sluten_lak and all these variables count to 120 variables, you could try making an array of these variables(opprim_lak--z_opprim_lak) and then using a do loop take them all.
If these variables are not in sequence then what you could do is, make a macro list using proc sql (where var like '%_lak%') separated by ' ' ( let say macro variable is &dummy) and then array nums
  • &dummy;
    If the dataset has these 120 variables as numeric data types and there is no other numeric type variables in the dataset(there can be character type vriables) then you could use array nums
  • _numeric_;

    example code:
    data dx;
    opprim_lak=.;
    sluten_lak=.;
    a_sluten_lak=.;
    z_opprim_lak=.;
    run;
    data new;
    set dx;
    array nums
  • opprim_lak--z_opprim_lak;
    do i=1 to hbound(nums);
    if nums=. then nums=0;
    end;
    drop i;
    run;
    proc print;run;
  • New Contributor
    Posts: 4

    Re: Is loop possible here?

    Thank you very much for your quick response. They are numeric and in a sequence but not the only numeric variables i have. I will try your suggestion.
    Thanks
    Janne
    Contributor
    Posts: 31

    Re: Is loop possible here?

    Hey Janne,
    i just saw that part of example code got skipped due to usage of Increment variable i within square brackets , and made last part of code into italic. Here is the fixed one

    data dx;
    opprim_lak=.;
    sluten_lak=.;
    a_sluten_lak=.;
    z_opprim_lak=.;
    run;
    data new;
    set dx;
    array nums{*} opprim_lak--z_opprim_lak;/*first var -- last var*/
    do i=1 to hbound(nums);
    if nums{i}=. then nums{i}=0;
    end;
    drop i;
    run;
    proc print;run;
    New Contributor
    Posts: 4

    Re: Is loop possible here?

    Thanks. I used your first suggestion and did like this:

    array vardvar{117} opbesok -- slutenovrig;
    do i=1 to 117;
    if vardvar{i}=. then vardvar{i}=0;
    end;

    and that workedSmiley Happy

    Saved me a lot of work!
    Ask a Question
    Discussion stats
    • 4 replies
    • 128 views
    • 1 like
    • 2 in conversation