Dollar General Corp. and Dollar Tree Inc. reported higher quarterly sales as the discount chains raised prices and attracted thrifty shoppers looking for groceries or other essentials.

Sales at dollar stores tend to rise when shoppers feel cash-strapped as the small stores tend to be located closer to residences and sell goods in small packages at lower prices points than some big-box competitors or grocery stores.

Dollar General said comparable sales, those from stores and digital channels operating at least 12 months, rose 4.6% in the quarter ended July 29 compared with the same period last year. More shoppers visited its stores and spent more on consumables such as food but less on apparel, seasonal goods and home products. Apparel sales dropped 21.4% in the quarter.

At Dollar Tree, which owns the Dollar Tree and Family Dollar chains, comparable sales rose 4.9% in the quarter ended July 30. The company said consumers are shifting spending to more need-based purchases such as food.

SHARE YOUR THOUGHTS

How has inflation changed the way you shop? Join the conversation below.

In recent weeks major retailers including Walmart Inc. and Target Corp. have noted that consumers are pulling back on discretionary spending, instead giving priority to food and fuel amid rising prices.

The dollar-chain competitors reported diverging profit estimates as Dollar Tree works to improve performance at Family Dollar, the chain it purchased in 2015 and has struggled to improve since.

Dollar Tree narrowed its net-sales expectations for the year and reduced its profit estimates, in large part because it lowered prices at Family Dollar to improve sales, the company said. That investment, along with shoppers’ reduced spending on higher-margin discretionary items, will lower margins for the year, the company said.

“We believe we are putting our best foot forward at a time when customers are coming to us to help them navigate difficult times,” said Dollar Tree Chief Executive Mike Witynski.

The company expects earnings-per-share to fall in a range of $7.10 to $7.40 for the full year, down from a previous estimate of $7.80 to $8.20.

Dollar Tree’s shares fell 7.5% to $153 in premarket trading, while Dollar General’s stock fell 2% to $242.

Dollar General raised its sales expectations for the year and maintained its profit outlook. It now expects comparable sales to rise 4% to 4.5%, up from a previous estimate of 3% to 3.5%.

Last quarter, Dollar General Chief Executive Todd Vasos

said the retailer saw signs that consumer habits were becoming more thrifty. The company said Mr. Vasos will step down as CEO and be succeeded by one of his lieutenants, Jeff Owens, on Nov. 1.

On Thursday, Dollar General said net sales rose 9% to $9.4 billion in the most recent quarter. Net income rose 6.4% to $678 million. Earnings per share were $2.98 during the quarter, beating analysts’ expectations of $2.94, according to FactSet.

Dollar Tree said net sales rose 6.7% to $6.77 billion during the most recent quarter. Net income rose 27.4% to $359.9 million. Earnings-per-share rose 30.1% to $1.60, in line with analysts’ estimates, according to FactSet.

Write to Sarah Nassauer at sarah.nassauer@wsj.com