BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
afiqcjohari
Quartz | Level 8

in data step

 

The following will create a new variable newvar based on myvar and drop myvar

 

data newtable(drop = myvar);
   length newvar %5;
   set table;
   if myvar = N then newvar = No
   if myvar = Y then newvar = Yes
run;

How can I replicate this in SAS proc sql?

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21
proc sql;
  create table newtable as
    select case(myvar)
      when ('N') then 'No'
      when ('Y') then 'Yes'
     end as newvar length=5
       from table
  ;
  quit;

Art, CEO, AnalystFinder.com

 

View solution in original post

6 REPLIES 6
afiqcjohari
Quartz | Level 8
That lexjansen is going to be very useful. Thanks Reeza!
art297
Opal | Level 21
proc sql;
  create table newtable as
    select case(myvar)
      when ('N') then 'No'
      when ('Y') then 'Yes'
     end as newvar length=5
       from table
  ;
  quit;

Art, CEO, AnalystFinder.com

 

afiqcjohari
Quartz | Level 8
Ok, I missed the length option. Thanks!
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

 

Just to add, when doing binary choices such as this its useful to use ifn/ifc functions to shrink the code (note this would not work for pass-through):

data newtable;
  set table;
  newvar=ifc(myvar="N","No","Yes");
run;

proc sql;
  create table NEWTABLE as
  select  ifc(MYVAR="N","No","Yes") as NEWVAR
  from    TABLE;
quit;
afiqcjohari
Quartz | Level 8
proc sql;
create table test as
select case
when myvar= 'N' then 'LCY'
when myvar = 'Y' then 'FCY'
end as newvar
from table
;quit;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 6 replies
  • 47249 views
  • 4 likes
  • 4 in conversation