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-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
    • 4 replies
    • 711 views
    • 1 like
    • 2 in conversation