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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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