BookmarkSubscribeRSS Feed
Mohana1
Calcite | Level 5
If i am having a column of computer science and engineering,maths and english,science and social,electronics engineering and maths ,how to separate a single column of variables into two columns before and after 'and'.
3 REPLIES 3
Quentin
Super User

Your question isn't clear.  Please provide the SAS code (data step code) to generate an example of the data you have, and then show the data you want.  Also please share the code you have tried.  

 

 

Mohana1
Calcite | Level 5
Variable name major contains observation as computer science and enginnering, maths and english, science and maths.the variable name major should contain only names before word 'and' such as 1.computer science 2.maths 3.science which is words before 'and' and another variable major2 should contain 1.engineering 2.English 3.maths which is after word 'and'.
Tom
Super User Tom
Super User

So first let's convert your words into an actual dataset.

data have;
  input major $60.;
cards;
computer science and enginnering
math and english
science and math
math
english
;

Now we can start writing some code to try to solve the problem.

data want;
  set have;
* Copy the data so SAS will guess that you want MAJOR1 and MAJOR2 defined 
  as the same type and length as MAJOR
;
  major1=major;
  major2=major;
* Check if the word AND appears ;
  location=findw(major,'and',' ','i');
* Split when found ; 
  if location then do;
    major1 = substr(major,1,location-1);
    major2 = left(substrn(major,location+4));
  end;
  else major2=' ';
run;

Tom_0-1671223518554.png

 

PS Clearly there aren't any English majors as they don't know how to spell ENGINEERING or MATH.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 721 views
  • 0 likes
  • 3 in conversation