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

Hi, anyone did this question and can help? I have two questions about the solution and code for the challenge practice on page87-88 of SQL1 essential pdf. (1) why do I get those brackets () for the pctincrease column in the output (see below)? I did not write any brackets() in my code. (2) in where statement, for some reason I cannot write the complete value("Borrowed for health or medical purposes (% age 15+)") of the indicatorname column (I do not think I had spelling or upcase or lowcase problems), if I write the complete value I got nothing  in the output table 

proc sql;
select region,
       (mean(estyear1pct)/100)*sum(estyear1pop) as estcount1
                        format=comma12.,
       (mean(estyear3pct)/100)*sum(estyear3pop) as estcount3
                        format=comma12.,
       (calculated estcount3-calculated estcount1)/calculated estcount1
                        as pctincrease format=percent7.2
   from sq.globalfull
   where indicatorname like "%health%"
   group by region
   order by pctincrease desc;
quit;

Untitled1.png

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

why do I get those brackets () for the pctincrease column in the output (see below)? I did not write any brackets() in my code.

You are using SAS format PERCENTw.d to print the numbers. This format prints negative numbers in brackets

Patrick_0-1739789121073.png

 

for some reason I cannot write the complete value("Borrowed for health or medical purposes (% age 15+)") of the indicatorname column

The percent sign % is also a SAS macro token. If you use it as a wildcard in a SQL like expression always use single quotes so SAS doesn't treat it as macro token (if you haven't gotten to SAS Macro language yet just take this use single quotes as a rule. You'll understand later the why).
The % in a SQL like expression is a wildcard for one or multiple characters. If you want to search for the actual character % then you should escape it as done in below sample code.  

The search as such for the full string works for me which makes me think that the search string you're using doesn't match the data (which includes whitespace characters like if you believe it's blank but what's stored is actually a tab).

data work.have;
  length indicatorname $60;
  indicatorname='Borrowed for health or medical purposes (% age 15+)'; output;
run;

proc sql;
  select *
  from work.have
  where indicatorname like 'Borrowed for health or medical purposes (^% age 15+)' escape '^'
  ;
  select *
  from work.have
  where indicatorname = 'Borrowed for health or medical purposes (% age 15+)'
  ;
quit;

Patrick_1-1739790110440.png

 


 

 

 

View solution in original post

4 REPLIES 4
Patrick
Opal | Level 21

why do I get those brackets () for the pctincrease column in the output (see below)? I did not write any brackets() in my code.

You are using SAS format PERCENTw.d to print the numbers. This format prints negative numbers in brackets

Patrick_0-1739789121073.png

 

for some reason I cannot write the complete value("Borrowed for health or medical purposes (% age 15+)") of the indicatorname column

The percent sign % is also a SAS macro token. If you use it as a wildcard in a SQL like expression always use single quotes so SAS doesn't treat it as macro token (if you haven't gotten to SAS Macro language yet just take this use single quotes as a rule. You'll understand later the why).
The % in a SQL like expression is a wildcard for one or multiple characters. If you want to search for the actual character % then you should escape it as done in below sample code.  

The search as such for the full string works for me which makes me think that the search string you're using doesn't match the data (which includes whitespace characters like if you believe it's blank but what's stored is actually a tab).

data work.have;
  length indicatorname $60;
  indicatorname='Borrowed for health or medical purposes (% age 15+)'; output;
run;

proc sql;
  select *
  from work.have
  where indicatorname like 'Borrowed for health or medical purposes (^% age 15+)' escape '^'
  ;
  select *
  from work.have
  where indicatorname = 'Borrowed for health or medical purposes (% age 15+)'
  ;
quit;

Patrick_1-1739790110440.png

 


 

 

 

dxiao2017
Lapis Lazuli | Level 10

Hi Patrick, thanks very much for your quick reply, accurately solves my problem. I did not notice that in the rows that have brackets, the value of estcount3-estcount1 was negative numbers and then I was thinking I would not get any answer by searching the internet or any help documents all the way on the internet (you even don't know what key word or question you can search for, isn't it). 

 

For the second problem, I tried both single and double quotation mark and still got nothing, and I think you are right about that the string doesn't match the data (for some reason, which need further explore and identifying), my code and the value in the input dateset are as follows.

Untitled1.png

Untitled2.png

The column and value in the input dataset is as follows. 

Untitled3.png

Both single and double quotation mark work when I use that where indicatorname like %health%; statement and what I get in the result is the same as the answer provided by the question. 

FreelanceReinh
Jade | Level 19

Hi @dxiao2017,

 

Isn't there a blank between "purposes" and "(% age 15+)", which is missing in your code?

with_blank.png

without_blank.png

dxiao2017
Lapis Lazuli | Level 10

Hi Reinh, you are right, solved my problem! I thought I paid sufficient attention to all details but while doing this question I actually was not. The code and result is as below. Double quotation mark also works and produced the same result.

proc sql;
select region,
       (mean(estyear1pct)/100)*sum(estyear1pop) as estcount1
                        format=comma12.,
       (mean(estyear3pct)/100)*sum(estyear3pop) as estcount3
                        format=comma12.,
       (calculated estcount3-calculated estcount1)/calculated estcount1
                        as pctincrease format=percent7.2
   from sq.globalfull
   where indicatorname='Borrowed for health or medical purposes (% age 15+)'
   group by region
   order by pctincrease desc;
quit;

Untitled3a.png

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Autotuning Deep Learning Models Using SAS

Follow along as SAS’ Robert Blanchard explains three aspects of autotuning in a deep learning context: globalized search, localized search and an in parallel method using SAS.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1825 views
  • 2 likes
  • 3 in conversation