简体   繁体   中英

SAS Macro Code Variable Creation

I'd like to create multiple flags in my dataset (and many derived numeric characteristics), but I am struggling to get the code to work.

I am using an iterative loop with conditions. Any help or suggestions on resolutions would be greatly appreciated.

Here is the code I tried, which creates a flag, but this is not then a variable in my new dataset.

%macro gb1(howmany);
   %do i=1 %to &howmany;
            %if status&i = 1 %then %let &gb1_&i = 1;
      %else %if status&i = 2 %then %let &gb1_&i = 1;
      %else %if dpd&i >= 2   %then %let &gb1_&i = 1;
      %else %if ((dpd&i < 2) and (dpd&i >= 1)) %then %let &gb1_&i = 2;
      %else %let &gb1_&i = 1;    
   %end;    
%mend gb1;

data test;
   set y2014.perf_data_derive (where=((dpd15 <= 2) and (prod = 1)));;
   %gb1(36);
run;

I appreciate any help I can get, thanks in advance.

%if , %then , %else etc. are macro statements and are used to evaluate expressions with macro variables in order to conditionally submit pieces of SAS code. It seems like you're not trying to do any of that; you want to just repeat data step statements that involve variables in your data set (not macro variables). To do this, use statements like you would in a data step:

%macro gb1(howmany);

%do i=1 %to &howmany;

if status&i = 1 then gb1_&i = 1;
else if status&i = 2 then gb1_&i = 1;
else if dpd&i >= 2 then gb1_&i = 1;
else if ((dpd&i < 2) and (dpd&i >= 1)) then gb1_&i = 2;
else let gb1_&i = 1;

%end;

%mend gb1;

I think you are confused about the macro language. It is used (generally) to generate SAS code. Basically, it saves you from typing. It does not do anything with DATA step variables. DATA step code lets you manipulate data step variables, the macro language deals with macro variables.

Does your input dataset perf_data_derive have variables like status1 status2 ... status36 and dpd1 dpd2 ... dpd36? And you are trying to create new variables gb1_1 gb1_2 ... gb1_36? If so, you should look into how to use arrays. The ARRAY statement is a DATA step statement that lets you refer to data step variables. You could code it something like (untested):

data test;
  set y2014.perf_data_derive (where=((dpd15 <= 2) and (prod = 1)));
  array status{36} ; *array elements status1-status36;
  array dpd{36} ;
  array gb1_{36} ; *array statement can create new variables! ;
  do i=1 to 36;
    if status{i}=1 then gb1_{i} = 1;
    else if status{i}=2 then gb1_{i} = 1;
    else if dpd{i} >=2 then gb1_{i} = 1;
    *etc;
  end;
  drop i;
run;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM