BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mlogan
Lapis Lazuli | Level 10

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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));

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

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));

Jagadishkatam
Amethyst | Level 16
left function will move the blank spaces in the beginning of the character variable day to right side. so that the values created will be like sport1,sport2,sport3..... if left is not used then there is a chance that the values created will be sport 1 , sport 2 ............. which will not resolve the macro variables to convert into values.
Thanks,
Jag

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to connect to databases in SAS Viya

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.

Discussion stats
  • 2 replies
  • 923 views
  • 1 like
  • 3 in conversation