BookmarkSubscribeRSS Feed
cody_q
Calcite | Level 5

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
3 REPLIES 3
Patrick
Opal | Level 21

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

data have (drop=_:);
  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;

cody_q
Calcite | Level 5

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 .

Patrick
Opal | Level 21

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=_:);
  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;

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 3 replies
  • 882 views
  • 0 likes
  • 2 in conversation