DATA Step, Macro, Functions and more

Creating dummy variables for one character variable

Reply
N/A
Posts: 1

Creating dummy variables for one character variable

I would like to convert a variable that combined all information about requirements for each statement and we need to make about 30 dummy variables (this is the number of special requirements). We have two types of such requirements: regular (R) and special (S). Every statement has R requirement(s) but not every has S requirements. For example for a statement: !S3, 4 !R22 which means this statement has two special requirements (#3 and #4) and one regular requirement (#22). Based on such information I need to convert it to S1=0, S2=0, S3=1, S4=1. S5=0 and R1=0, R2=0, ..., R21=0, R22=1, R23=0, ..., R25=0.
Note. the code for the type of requirement starts with ! and separate among the requirement of same type by a comma and between the two types of requirements by a space.

Could you please help me achieve such task?
Super User
Posts: 17,963

Re: Creating dummy variables for one character variable

Lookup the scan function.

s_comp=scan (var, 1, "!");
r_comp=scan(var, 2, "!");

This assumes that R and S are always in the order stated.

Use it once to separate the S and R components into separate strings, then in each string you can scan or using "S," or "R," as the delimiter and a loop to get the numbers.
Super Contributor
Super Contributor
Posts: 3,174

Re: Creating dummy variables for one character variable

I agree with Reeza, and suggest the "input record buffer" be parsed using one of a few different DATA step functions, likely considering maybe SCAN, FIND, SUBSTR, INDEX, INDEXC, and possibly VVALUE or VVALUEX for assignment.

Suggested DATA step structure approach demonstrated below:

DATA ;
KEEP ....;
* RECOMMEND CODING SAS ATTRIB OR LENGTH STATEMENTS ;
* HERE TO DECLARE EXPECTED SAS VARIABLES. ;
INFILE ....;
INPUT ;
DO WHILE(1=1);
* COME HERE FOR INFINITE LOOP TO PARSE CURRENT INPUT ;
* RECORD AND DECODE ONE OR MORE SUB-FIELDS. ;
LENGTH TEMPVAR $1000;
I+1;
TEMPVAR = SCAN(_INFILE_,I,'!');
* EXIT THE DO LOOP NOW WHEN NO MORE FIELDS TO PARSE. ;
IF TEMPVAR = ' ' THEN LEAVE;
* YOUR PARSING CODE FOR EACH RECORD SUBTYPE/FIELDS GOES HERE.;
END;
RUN;
Ask a Question
Discussion stats
  • 2 replies
  • 370 views
  • 0 likes
  • 3 in conversation