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.  

 

 

The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
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.

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!

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
  • 455 views
  • 0 likes
  • 3 in conversation