I work with SAS and the other members of my team work with SPSS and I usually need to translate codes between SPSS and SAS. I wonder if someone may help me to translate this. I want to create new_var from var
I appreciate any hint that may help me to translate this. Thanks
STRING new_var (A200).
EXECUTE.
COMPUTE new_var=CHAR.SUBSTR(var,1,CHAR.INDEX(var,"?")-1).
IF NOT(CHAR.INDEX(var,"?")>0) new_var = var.
EXECUTE.
@Emma_at_SAS wrote:
I work with SAS and the other members of my team work with SPSS and I usually need to translate codes between SPSS and SAS. I wonder if someone may help me to translate this. I want to create new_var from var
I appreciate any hint that may help me to translate this. Thanks
STRING new_var (A200).
EXECUTE.
COMPUTE new_var=CHAR.SUBSTR(var,1,CHAR.INDEX(var,"?")-1).
IF NOT(CHAR.INDEX(var,"?")>0) new_var = var.
EXECUTE.
No one said what the code is supposed to do?
Looks like it is just trimming the data to the the first part before the first question mark. You could try to translate the existing logic into SAS, but why not just use the SCAN() function?
length new_var $200;
new_var=scan(var,1,'?');
Hello Mary,
I think I can guess approximately what this SPSS code does, but it would be better if you can explain it a little bit.
Or specify some relevant examples of "var" and "new_var".
That will make it easier for the majority of us to answer correctly.
Thanks,
Koen
Thank you @sbxkoenk. Each observation for var is a text and now I know I need to cut the new_var to the portion before the ? mark (only including the text up to the ? mark). Thanks!
@Emma_at_SAS wrote:
I work with SAS and the other members of my team work with SPSS and I usually need to translate codes between SPSS and SAS. I wonder if someone may help me to translate this. I want to create new_var from var
I appreciate any hint that may help me to translate this. Thanks
STRING new_var (A200).
EXECUTE.
COMPUTE new_var=CHAR.SUBSTR(var,1,CHAR.INDEX(var,"?")-1).
IF NOT(CHAR.INDEX(var,"?")>0) new_var = var.
EXECUTE.
No one said what the code is supposed to do?
Looks like it is just trimming the data to the the first part before the first question mark. You could try to translate the existing logic into SAS, but why not just use the SCAN() function?
length new_var $200;
new_var=scan(var,1,'?');
Literal translation if I remember my SPSS correctly:
length new_var $ 200; if index(var,'?')>0 then new_var = substr(var,1,index(var,'?')-1); else new_var=var;
However if the purpose is to get everything before a single ? in the data
new_var= scan(var,1,'?');
The '?' tells SAS to only consider a question mark as a delimiter between string sections. If one is not found then the entire string is returned. New_var would inherit the length of the VAR variable if that is the first use. If that isn't desired then use a length statement to set an explicit length.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.