BookmarkSubscribeRSS Feed
SASEG
Calcite | Level 5
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?
2 REPLIES 2
Reeza
Super User
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.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1733 views
  • 0 likes
  • 3 in conversation