BookmarkSubscribeRSS Feed
CherylA
Calcite | Level 5

Hi, I have a string variable of up to length 24 which represents different scores (from 0-9) for 24 months. 

Var = "0000000134564388889..." 

I need to create 72 variables that are 1 or 0. 

i.e score_1 will be 1 if the first number in the Var string is >=1 and 0 if not. 

score_2 will be 1 if the second number in the var string is >=1 and 0 if not, 

and so on. 

 

I'll then sum the variables for each month using something like this to get a sum of the observations that were >=1 at each month: 

proc summary data = data (keep=score_1-score_24) nway missing ;
var score_1-score_22;
output out = data_out (drop= _type_) sum= ;
run ;

 

Thanks in advance! 

2 REPLIES 2
Astounding
PROC Star

You will need to explain a little more.  If you only have 24 months of data, why do you need 72 variables?  What would your first 7 new variables look like if you were working with a string that began with:

 

Var = ">=1>=14 .....";

ballardw
Super User

@CherylA wrote:

Hi, I have a string variable of up to length 24 which represents different scores (from 0-9) for 24 months. 

Var = "0000000134564388889..." 

I need to create 72 variables that are 1 or 0. 

i.e score_1 will be 1 if the first number in the Var string is >=1 and 0 if not. 

score_2 will be 1 if the second number in the var string is >=1 and 0 if not, 

and so on. 

 

I'll then sum the variables for each month using something like this to get a sum of the observations that were >=1 at each month: 

proc summary data = data (keep=score_1-score_24) nway missing ;
var score_1-score_22;
output out = data_out (drop= _type_) sum= ;
run ;

 

Thanks in advance! 


Here is one way.

data junk;
   Var = "0000000134564388889";
   array score_(24);
   do i=1 to length(var);
      score_[i] = (input(substr(var,i,1),best.)>1);
   end;
   drop i;
run;

SAS comparisons will return 1 for true and 0 for false so after pulling the value, converting to numeric with input and substr, the comparison yields what was requested.

 

But again why 72 variables?

 

You may find it preferable to reshape the data to have a MONTH Value instead of hiding that information in a variable name. Which would be relatively easy for the example data.

data junk2;
   Var = "0000000134564388889";
   do month=1 to length(var);
      score_ = (input(substr(var,month,1),best.)>1);
      output;
   end;
   drop var;
run;

Your summary would use MONTH as a class or by variable, which would then have the NWAY actually do something.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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