BookmarkSubscribeRSS Feed
dustychair
Pyrite | Level 9

Hi all,

I have a string variable that has items' scores. Each score has two fields. For example, 

"010002 M0102 Z" in this row there are 7 items' scores, the first item's score is "01" and the last one is " Z" (there is space before Z since it should have two fields). I want to recode Z's and M's as 0 or missing depending on their locations.

1. If M or Z is at the end then they should be  missing. If they are followed by a score then they will be recoded as "00".

2. No matter where Zs are located Zs should be missing.

 

I searched for similar questions but not sure how to handle the location part.

 

Thank you,

 

4 REPLIES 4
ballardw
Super User

First a general comment: any single variable that holds "7 scores" is likely a problem. Variables should hold a single value. Second, you need to show some examples of the result. There are several ways to interpret "should be missing".

Consider a potential value of

"010002 Z0102 Z".

Does setting the first Z to "missing" result in

"010002  0102 Z" (2 spaces between the 2 and 0)  "010002 0102 Z" (1 space between the 2 and 0) or "010002 .0102 Z" (the default . as "missing" value character?

 

If you want to replace inside the variable you may have a problem. You say that some M should be replaced with "00". Or do you mean that " M", a leading space and M, should be replaced by "00"? That is two different things.

 

dustychair
Pyrite | Level 9

Hi @ballardw,
Zs and Ms hold two fields. So it is like " Z" (there is 1 space before Z). I want to recode it as " "( there are two spaces. My plan is to separate them. Actually I already have done it. So, I want to code something like : if the last variable has Z or M then do " ". if any variable has Z then do " " else any other variables has M then do 00. Or I am not sure if it is a good idea to separate them. Since the last response order might be different for some students. It is easy in wording but hard in coding 🙂

Thanks.

ballardw
Super User

Maybe easier with the long string value. See if this gives you something that works.

 

data example;
   str="010002 Z0102 Z";
   /*replace ALL Z with missing*/
   str=translate(str,' ','Z');
   str2='010002 M0102 M';
   If countc(str2,'M')>0 then do i= 1 to countc(str2,'M');
      pos=index(str2,'M');
      if pos < length(str2) then str2=tranwrd(str2," M","00");
      else str2=translate(str2,' ','M');
   end;
   drop pos i;
run;

I handle the Z and M differently because you have different rules. The Translate function replaces characters that appear in the third parameter with the corresponding positional value in the second parameter. You can change many characters this way but also works for just one. The Tranwrd function replaces the value of the second parameter with the entire value of the third if present. The loop is a possibly unstated requirement to handle up to 7 M values. The Countc function returns how many characters are found.

 

If have taken your statement for Z as the 2. No matter where Zs are located Zs should be missing. Which does contradict the rule 1 as far as Z values go.

 

 

dustychair
Pyrite | Level 9

The code itself is working but I don't think it is working properly. For example,

0101010000 M M M M M M M is recoded as

010101000000000000000000

So, it has converted all of the Ms to 00. However, the last M should be missing. 

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