Desktop productivity for business analysts and programmers

Writing Macro in SAS EG

Reply
Contributor
Posts: 24

Writing Macro in SAS EG

Hey all,


I have troubles writing my own macro. I have been given a rough guide to try out but i think there's a problem with the logic.
f1 is a column and f2 is another column .
I want to rename them as a column named email.


data f1 (keep=f1 rename=f1=email) f2 (keep=f2
rename=f2=email);
set WORK.TEXT_14;
run;

data all;
set f1 f2;
run;

The macro is named email . To my understanding , that is to get i=1 as the first column to &cnt ( not knowing where is the end column)
%macro email;
data
%do i=1 to &cnt;
&&data&1
(keep=&&data&1
rename=&&data&1=email)
%end;
;
set
WORK.TEXT_14;
run;

data
all;
set
%do i=1 to &cnt;
&&data&1
%end;
;
if
email ne '';
run;
%mend;
%email;
My outcome is to get the original dataset of F1 to F92 columns into each table on its own and then append to a column named email.
However, hard coding will be too tiedous.
Thank you
Respected Advisor
Posts: 3,840

Re: Writing Macro in SAS EG

If I understand your problem correct then you don't need macro coding at all. See below:

data have (drop=_Smiley Happy;
  array F {20} 8.;
  do _i=1 to 10;
    do _j=1 to dim(F);
      f(_j)=ceil(ranuni(0)*10);
    end;
    output;
  end;
run;

data want(keep=email);
  set have;
  array vars {*} _numeric_;
  do _i=1 to dim(vars);
    email=vars(_i);
    output;
  end;
run;

Contributor
Posts: 24

Re: Writing Macro in SAS EG

Hey Patrick , Supposedly i have 10 columns (see below)

FILE A

F1 to F10 are the columns.


 

F1


 

 

F2


 

 

F3


 

 

F4


 

 

F5


 

 

F6


 

 

F7


 

 

F8


 

 

F9


 

 

F10


 

 

There


 

 

are


 

 

40


 

 

students


 

 

in


 

 

the


 

 

class


 

 

.


 

 

They


 

 

are


 

 

all


 

 

studying


 

 

for


 

 

Math


 

 

and


 

 

Science


 

 

.


 

 

They


 

 

have


 

 

learnt


 

 

valuable


 

 

lessons


 

 

from


 

 

the


 

 

Classes.

 

 
The
 

  teachers
 

  are
 

  proud
 

  of
 

  them
 

  .
 

  
 

  
 

  
 

  
 

  
 

  
 

  
 

  
 

      

FILE B

Become to F1 as the only column with many rows.


  F1
 

  There
 

  are
 

  40
 

  students
 

  in
 

  the
 

  class
 

  .
 

  They
 

  are
 

  all
 

  studying
 

  for
 

  Math
 

  and
 

 

Science.

 

  They
 

  have
 

  learnt
 

  valuable
 

  lessons
 

  from
 

  the
 

 

classes.

 

 

The


 

 

teachers


 

 

are


 

 

proud


 

 

of


 

 

them


 

 

.


 

The codes which you have written . Converted the dataset of File A to numerics and then File B to just 1 column with many rows. Yes, this is the expected outcome but i am looking for the words and not numeric . Could you help in the codes you have written. FYI, my real dataset has 92 columns with hundreds of rows.

Thank you .

Respected Advisor
Posts: 3,840

Re: Writing Macro in SAS EG

Assuming all your variables are character a simple change to the code will do. Make sure to define a length for email which is as long as the longest of your input character variables.

data have (drop=_Smiley Happy;
  array F {20} $20.;
  do _i=1 to 10;
    do _j=1 to dim(F);
      f(_j)=cats('ABC',ceil(ranuni(0)*10));
    end;
    output;
  end;
run;

data want(keep=email);
  set have;
  array vars {*} _character_;
  length email $ 20;
  do _i=1 to dim(vars);
    email=vars(_i);
    output;
  end;
run;

Ask a Question
Discussion stats
  • 3 replies
  • 183 views
  • 0 likes
  • 2 in conversation