DATA Step, Macro, Functions and more

Using Macro to create new variables

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 104
Accepted Solution

Using Macro to create new variables

Hi SAS users,

I'm working on a macro where I'd like to 1) re-code values and 2)convert those values from character to numeric. As I have over 20 variables that require re-coding and convert, I'm hoping to use a macro to make it as efficient as possible.  Thus far, I've only been able to figure out how to do one variable at a time within the data step.  Any suggestions?

Thanks!

data have;

input StudyID $1-2 CaseID $4-6 m2blue $8-12 q1green $14-18 k5orange $20-24;

datalines;

AA AA1 0-No  0-No  1-Yes

BB BB1 0-No  0-No  0-No

CC CC1 1-Yes 0-No  1-Yes

DD DD1 0-No  1-Yes 1-Yes

EE EE1 1-Yes 1-Yes 1-Yes

FF FF1 0-No  0-No  0-No

GG GG1 0-No  0-No  0-No

HH HH1 0-No  1-Yes 0-No

II II1 1-Yes 0-No  0-No

JJ JJ1 1-Yes 0-No  1-Yes

;;

run;  

data bugn;

input vd $ 1-40;

datalines;

m2blue

q1green

k5orange

;

run;

data _null_;

set bugn end=last;

call symput('vd'||left(put(_n_,3.)),vd);

if last then call symput('bugn',_n_);

run;

options mprint;

%macro loop;

%do i=1 %to &bugn;

%let ti=%cmpres(&bug&i);

data two;

vd="&&vd&i";

run;

%let vd=&&vd&i;

data want;

set have (rename=(&vd=&vd._old));

format &vd 1.;

    %do i=1 %to &bugn;

if substr(&vd._old,1,1) = '0' then &vd=0;

    else if substr(&vd._old,1,1) = '1' then &vd=1;

     retain &vd;                                                             

    keep &vd;

%end;

run;

%end;

%mend loop;

%loop;


Accepted Solutions
Solution
‎10-08-2014 03:48 PM
Super User
Posts: 10,497

Re: Using Macro to create new variables

With a text file you can specify a custom format on reading the data.

I am guessing that you want 1 for Yes and 0 for No in your final form.

Proc format;

invalue MyYesNo

'1-Yes' = 1

'0-No' = 0

;

run;

data want;

     infile "your file name to read" <options to read>; /* if you used proc import you can look at the resulting code in the log to get this */

     informat  m2blue q1green  k5orange  MyYesNo. ;

    input StudyID $1-2 CaseID $4-6 m2blue 8-12 q1green 14-18 k5orange 20-24; /* what ever the input statement needs*/

run;

View solution in original post


All Replies
Super User
Posts: 17,819

Re: Using Macro to create new variables

Are you originally reading from a text file or a SAS data set?

Frequent Contributor
Posts: 104

Re: Using Macro to create new variables

The data is originally from a text file.  What would be the difference if it was from a SAS data set?

Super User
Posts: 17,819

Re: Using Macro to create new variables

Fix it in the original import step rather than try and do it afterwards.  i.e. fix it before it's broken.

Can you post a small sample of the text file or is the data step above representative? If so, what would you want the output to be.

Solution
‎10-08-2014 03:48 PM
Super User
Posts: 10,497

Re: Using Macro to create new variables

With a text file you can specify a custom format on reading the data.

I am guessing that you want 1 for Yes and 0 for No in your final form.

Proc format;

invalue MyYesNo

'1-Yes' = 1

'0-No' = 0

;

run;

data want;

     infile "your file name to read" <options to read>; /* if you used proc import you can look at the resulting code in the log to get this */

     informat  m2blue q1green  k5orange  MyYesNo. ;

    input StudyID $1-2 CaseID $4-6 m2blue 8-12 q1green 14-18 k5orange 20-24; /* what ever the input statement needs*/

run;

Frequent Contributor
Posts: 104

Re: Using Macro to create new variables

Thanks!  This worked great and was a lot easier than a macro.

Super User
Posts: 10,497

Re: Using Macro to create new variables

Please mark the question as answered so people know we don't need to check whether this still needs an answer.

Super User
Posts: 5,081

Re: Using Macro to create new variables

You could just change your INPUT statement and avoid writing macros entirely:

length dummy $ 1;

drop dummy;

input StudyID $1-2 CaseID $4-6 m2blue : 1. dummy q1green : 1. dummy k5orange : 1.;


Contributor
Posts: 49

Re: Using Macro to create new variables

Hi Astounding,

what does it means the ':' carachter in the input statement, before the number?

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 358 views
  • 1 like
  • 5 in conversation