BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Emma_at_SAS
Lapis Lazuli | Level 10

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@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,'?');

View solution in original post

5 REPLIES 5
sbxkoenk
SAS Super FREQ

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

Emma_at_SAS
Lapis Lazuli | Level 10

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! 

sbxkoenk
SAS Super FREQ

OK. Fine.

I guess your question was sufficiently answered by @Tom and @ballardw .

If indeed so, you can mark it as 'solved'.

Thanks,

Koen

Tom
Super User Tom
Super User

@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,'?');
ballardw
Super User

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: 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
  • 5 replies
  • 784 views
  • 2 likes
  • 4 in conversation