BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
owenwqp1
Obsidian | Level 7

Hi,

    I use the posted code to generate new vars, but after running the code, I just got one new variable, y20, what's wrong with my code?

Thanks a lot!

%macro abc;
    %do i=1 %to 20;
    data code1;
        set code;
        y&i=x&i/2;
    run;
    %end;
    %mend;
%abc

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

It is possible to use macro language to generate 20 assignment statements within the same DATA step.  You just need to rearrange the pieces of your program:

 

%macro abc;
   data code1;
      set code;
      %do i=1 %to 20;
          y&i=x&i/2;
      %end;
   run;
%mend abc;
%abc

View solution in original post

4 REPLIES 4
owenwqp1
Obsidian | Level 7

RW9
Diamond | Level 26 RW9
Diamond | Level 26

More importantly, why are you creating 20 datasets for the same data?  This uses more disk space, is slower, and is far harder to maintain.  I am pretty certain after seeing this type of code a lot, you will then either be merging those datasets, or looping over them using further messy macro code.  Don't.  You can simply do one step:

data code1 (drop=i);
  set code;
  array y{20} 8.;
  array x{8};   /* assumes it already exists in code */
  do i=1 to 20;
    y{i}=x{i}/2;
  end;
run;

Or you could create a normalised version of the above with each item in a row rather than columns which is a better structure.

 

Astounding
PROC Star

It is possible to use macro language to generate 20 assignment statements within the same DATA step.  You just need to rearrange the pieces of your program:

 

%macro abc;
   data code1;
      set code;
      %do i=1 %to 20;
          y&i=x&i/2;
      %end;
   run;
%mend abc;
%abc

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1187 views
  • 1 like
  • 4 in conversation