Hi all,
I copied the following code from internet. Can someone please tell me what this LEFT function (see below in bold) do here? I actually don't know the use of left function. Thanks,
/*Duration of exercise each day of week;*/
%LET time1=2.0;
%LET time2=1.0;
%LET time3=0.5;
%LET time4=0.6;
%LET time5=0.2;
%LET time6=1.0;
%LET time7=1.2;
/*Exercise planned for each day of week ;*/
%let sport1 =bike;
%let sport2 =walk;
%let sport3 =jog;
%let sport4 =stretch;
%let sport5 =bike;
%let sport6 =swim;
%let sport7 =walk;
DATA eachday
wholeweek (KEEP = calsofar timesofar RENAME = (calsofar=calperweek timesofar=hrsperweek));
DO day = 1 TO 7;
LENGTH sport intensity $8 ;
sport = SYMGET('sport'||LEFT(day)) ;
IF sport IN ('bike','swim','jog') THEN intensity = 'hard';
ELSE IF sport IN ('walk','dance') THEN intensity = 'moderate' ;
ELSE IF sport IN ('stretch','golf') THEN intensity = 'easy' ;
cal_num = INPUT(SYMGET(intensity),4.) ;
time = INPUT(SYMGET('time'||LEFT(day)),6.1) ;
totalcal = time*cal_num ;
calsofar = SUM(calsofar, totalcal) ;
timesofar = SUM(timesofar, time) ;
RETAIN calsofar timesofar ;
OUTPUT eachday ;
IF day = 7 THEN OUTPUT wholeweek ;
END;
RUN;
The LEFT() function left aligns the text in a character string. So basically it moves the leading blanks to trailing blanks.
In this case the programming is taking advantage of SAS's ability to automatically convert a numeric varaible DAY to a character string. But since SAS uses the BEST12. format when it does that there will be leading spaces in the result.
If you were wrting this code now you would use the CATS() function. This will avoid the need for the LEFT() funcaiton and also the annoying warning about automatic converstion.
sport = SYMGET(cats('sport',day));
The LEFT() function left aligns the text in a character string. So basically it moves the leading blanks to trailing blanks.
In this case the programming is taking advantage of SAS's ability to automatically convert a numeric varaible DAY to a character string. But since SAS uses the BEST12. format when it does that there will be leading spaces in the result.
If you were wrting this code now you would use the CATS() function. This will avoid the need for the LEFT() funcaiton and also the annoying warning about automatic converstion.
sport = SYMGET(cats('sport',day));
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.
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.