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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

8 REPLIES 8
Reeza
Super User

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

sophia_SAS
Obsidian | Level 7

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

Reeza
Super User

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.

ballardw
Super User

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;

sophia_SAS
Obsidian | Level 7

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

ballardw
Super User

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

Astounding
PROC Star

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.;


Rakeon
Quartz | Level 8

Hi Astounding,

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

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 8 replies
  • 1135 views
  • 1 like
  • 5 in conversation