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?
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
That's not valid SAS code to start with...
As always, see the documentation or search at lexjansen.
http://www.lexjansen.com/search/searchresults.php?q=Intro%20to%20proc%20sql
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
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;
proc sql; create table test as select case when myvar= 'N' then 'LCY' when myvar = 'Y' then 'FCY' end as newvar from table ;quit;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.