BookmarkSubscribeRSS Feed
janne
Calcite | Level 5
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"?
4 REPLIES 4
SushilNayak
Obsidian | Level 7
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;
  • janne
    Calcite | Level 5
    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
    SushilNayak
    Obsidian | Level 7
    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;
    janne
    Calcite | Level 5
    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 worked:)

    Saved me a lot of work!

    SAS Innovate 2025: Save the Date

     SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

    Save the date!

    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.

    SAS Training: Just a Click Away

     Ready to level-up your skills? Choose your own adventure.

    Browse our catalog!

    Discussion stats
    • 4 replies
    • 979 views
    • 1 like
    • 2 in conversation